简体   繁体   English

熊猫计算一栏中值出现的次数

[英]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_countsto_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.

相关问题 计算pandas数据框中逐列的出现次数 - count occurrences of number by column in pandas data frame 如何使用 pandas 计算另一列中每个值在一列中出现的次数? - How to count number of occurrences of value in one column per value in other column using pandas? Pandas:有没有办法计算给定列中值的出现次数,其中包含单元格中的字典值? - Pandas : Is there a way to count the number of occurrences of values in a given column which contains dictionaries values in the cells? 如何计算 Python Pandas 中逗号分隔列的出现次数 - How to count the number of occurrences on comma delimited column in Python Pandas pandas:计算列表中每个元素在列表列中唯一出现的次数 - pandas: count the number of unique occurrences of each element of list in a column of lists 在 pandas 中的条件之后计算列中特定文本的出现次数 - Count the number of occurrences of a specifc text in a column after a condition in pandas Python Pandas:将列值设置为另一列的出现次数 - Python Pandas : Set column values as number of occurrences of another column 如何计算熊猫列表列中值的总出现次数? - How count total occurrences of values in a column of lists in pandas? 熊猫-计算并从一列中获取唯一的字符串值 - Pandas - Count and get unique occurrences of string values from a column 快速计算熊猫列表中一列值的总出现次数的方法? - Faster way to count total occurrences of values in a column of lists in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM