[英]pandas count number of occurrences of values in one column
I have a long dataframe with only one column and around 800k rows. 我有一个长的数据框,只有一列,大约有80万行。 my data frame looks like this
我的数据框看起来像这样
54
53
53
53
53
...
0
0
0
So what I need is to count the number of occurrences of each value and saving this into a dataframe so the result would be something like this 所以我需要计算每个值的出现次数并将其保存到数据帧中,这样结果将是这样的
54 1
53 1000
52 800
...
0 100000
I have tried using df.groupby(0)
but it only returns an object. 我尝试使用
df.groupby(0)
但它仅返回一个对象。 How can I get a two-column dataframe (or 1 column and a row-index showing the values)? 如何获得两列数据框(或1列和显示值的行索引)?
Use value_counts
and to_frame
: 使用
value_counts
和to_frame
:
df = pd.DataFrame([1,2,4,5,5], columns=['values'])
df['values'].value_counts().to_frame().reset_index().rename(columns={'index':'values', 'values':'count'})
values count
0 5 2
1 4 1
2 2 1
3 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.