pandas - 数据框中出现的唯一行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40551335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:25:13  来源:igfitidea点击:

pandas - number of unique rows occurrences in dataframe

pythonpandas

提问by Pepacz

How can I count number of occurrences of each unique row in a DataFrame?

如何计算 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

And I would like to obtain

我想获得

   x1  x2 count 
0   A   1     2
1   A   2     3
2   A   3     1
3   B   3     2

回答by EdChum

IIUC you can pass param as_index=Falseas an arg to groupby:

IIUC 您可以将 paramas_index=False作为 arg传递给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

回答by Vlados

You could also drop duplicated rows:

您还可以删除重复的行:

In [4]: df.shape[0]
Out[4]: 8

In [5]: df.drop_duplicates().shape[0]
Out[5]: 4