pandas -数据框中唯一行的出现次数 [英] pandas - number of unique rows occurrences in dataframe
本文介绍了 pandas -数据框中唯一行的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算DataFrame
中每个唯一行的出现次数?
How can I count number of occurrences of each unique row in a DataFrame
?
df = {'x1': ['A','B','A','A','B','A','A','A'], 'x2': [1,3,2,2,3,1,2,3]}
df = pd.DataFrame(df)
df
x1 x2
0 A 1
1 B 3
2 A 2
3 A 2
4 B 3
5 A 1
6 A 2
7 A 3
我想获得
x1 x2 count
0 A 1 2
1 A 2 3
2 A 3 1
3 B 3 2
推荐答案
IIUC,您可以将参数as_index=False
作为arg传递给
IIUC you can pass param as_index=False
as an arg to groupby
:
In [100]:
df.groupby(['x1','x2'], as_index=False).count()
Out[100]:
x1 x2 count
0 A 1 2
1 A 2 3
2 A 3 1
3 B 3 2
这篇关于 pandas -数据框中唯一行的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文