计算数据帧pandas中唯一行的数量

问题描述 投票:1回答:2

我需要计算数据帧熊猫中唯一行的数量。我尝试这个解决方案:pandas - number of unique rows occurrences in dataframe但它会产生错误。

这是我尝试的代码:

import pandas as pd

df = {'x1': ['A','B','A','A','B','A','A','A'], 'x2': [1,3,2,2,3,1,2,3]}
df = pd.DataFrame(df)

print df.groupby(['x1','x2'], as_index=False).count()

这是错误:

Traceback (most recent call last):
  File "/home/user/workspace/project/test.py", line 9, in <module>
    print df.groupby(['x1','x2'], as_index=False).count()
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 4372, in count
    return self._wrap_agged_blocks(data.items, list(blk))
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 4274, in _wrap_agged_blocks
    index = np.arange(blocks[0].values.shape[1])
IndexError: list index out of range

我究竟做错了什么?

python pandas dataframe
2个回答
2
投票

通过使用size(ps:你可以在最后添加.reset_index())来做到这一点

df.groupby(['x1','x2'], as_index=False).size()
Out[1262]: 
x1  x2
A   1     2
    2     3
    3     1
B   3     2
dtype: int64

或者修复你的代码

df.groupby(['x1','x2'])['x2'].count()
Out[1264]: 
x1  x2
A   1     2
    2     3
    3     1
B   3     2
Name: x2, dtype: int64

如果您想了解唯一的组,可以使用ngroups

df.groupby(['x1','x2']).ngroups
Out[1267]: 4

1
投票

您可以删除重复项:

import pandas as pd

df = {'x1': ['A','B','A','A','B','A','A','A'], 'x2': [1,3,2,2,3,1,2,3]}
df = pd.DataFrame(df)

print(len(df.drop_duplicates()))

返回

4
© www.soinside.com 2019 - 2024. All rights reserved.