pandas groupby count string occurrence over column

孤街浪徒 提交于 2019-12-17 16:46:23

问题


I want to count the occurrence of a string in a grouped pandas dataframe column.

Assume I have the following Dataframe:

catA    catB    scores
A       X       6-4 RET
A       X       6-4 6-4
A       Y       6-3 RET
B       Z       6-0 RET
B       Z       6-1 RET

First, I want to group by catA and catB. And for each of these groups I want to count the occurrence of RET in the scores column.

The result should look something like this:

catA    catB    RET
A       X       1
A       Y       1
B       Z       2

The grouping by two columns is easy: grouped = df.groupby(['catA', 'catB'])

But what's next?


回答1:


Call apply on the 'scores' column on the groupby object and use the vectorise str method contains, use this to filter the group and call count:

In [34]:    
df.groupby(['catA', 'catB'])['scores'].apply(lambda x: x[x.str.contains('RET')].count())

Out[34]:
catA  catB
A     X       1
      Y       1
B     Z       2
Name: scores, dtype: int64

To assign as a column use transform so that the aggregation returns a series with it's index aligned to the original df:

In [35]:
df['count'] = df.groupby(['catA', 'catB'])['scores'].transform(lambda x: x[x.str.contains('RET')].count())
df

Out[35]:
  catA catB   scores count
0    A    X  6-4 RET     1
1    A    X  6-4 6-4     1
2    A    Y  6-3 RET     1
3    B    Z  6-0 RET     2
4    B    Z  6-1 RET     2


来源:https://stackoverflow.com/questions/31649669/pandas-groupby-count-string-occurrence-over-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!