我需要计算 pandas 数据框中唯一行的数量。我已经尝试过这个解决方案,但它会产生错误。
这是我的代码:
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()
Run Code Online (Sandbox Code Playgroud)
这是错误:
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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?