简体   繁体   English

Python Pandas:计算序列中的元素出现次数

[英]Python Pandas: Counting element occurrences in series

How to find series element counts? 如何找到系列元素计数? Using this code: 使用此代码:

import pandas as pd

d = { 'x' : [1,2,2,2,3,4,5,5,7] }
df = pd.DataFrame(d)
cnt1 = len(df[df.x == 1])
cnt2 = len(df[df.x == 2])
cnt3 = len(df[df.x == 3])
...

does not help much. 并没有太大帮助。 Is there any way to count element occurrences so result would be a dictionary with 'element, count' pairs, like this: 有什么方法可以计数元素出现的次数,因此结果将是带有“元素,计数”对的字典,如下所示:

cnts = {'1':1, '2': 3, '3':1, ...}

or in some other structure easy to lookup and iterate ? 还是以其他易于查找和迭代的结构?

You can use value_counts . 您可以使用value_counts It returns a Series which can be looked up like a dictionary and you can iterate over it: 它返回一个可以像字典一样查找的Series,并且可以对其进行迭代:

df['x'].value_counts(sort=False)
Out: 
1    1
2    3
3    1
4    1
5    2
7    1
Name: x, dtype: int64

If you want, you can convert it to a dictionary too: 如果需要,也可以将其转换为字典:

df['x'].value_counts().to_dict()
Out: {1: 1, 2: 3, 3: 1, 4: 1, 5: 2, 7: 1}

Here are two ways to get the freq-distribution 这是获得频率分布的两种方法

In [8]: df.groupby('x').size().to_dict()
Out[8]: {1: 1, 2: 3, 3: 1, 4: 1, 5: 2, 7: 1}

In [9]: df['x'].value_counts().to_dict()
Out[9]: {1: 1, 2: 3, 3: 1, 4: 1, 5: 2, 7: 1}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM