Python 熊猫,分组和计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47320572/
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-08-19 18:09:46  来源:igfitidea点击:

Pandas, groupby and count

pythonpandaspandas-groupby

提问by GoingMyWay

I have a dataframe say like this

我有一个像这样说的数据框

>>> df = pd.DataFrame({'user_id':['a','a','s','s','s'],
                    'session':[4,5,4,5,5],
                    'revenue':[-1,0,1,2,1]})

>>> df
   revenue  session user_id
0       -1        4       a
1        0        5       a
2        1        4       s
3        2        5       s
4        1        5       s

And each value of session and revenue represents a kind of type, and I want to count the number of each kind say the number of revenue=-1and session=4of user_id=ais 1.

和会话和收入的每个值代表了一种类型的,我要统计每个种类的数量表示的数量revenue=-1session=4user_id=a为1。

And I found simple call count()function after groupby()can't output the result I want.

count()groupby()无法输出我想要的结果后,我发现了简单的调用函数。

>>> df.groupby('user_id').count()
         revenue  session
user_id
a              2        2
s              3        3

How can I do that?

我怎样才能做到这一点?

回答by WNG

You seem to want to group by several columns at once:

您似乎想一次按几列分组:

df.groupby(['revenue','session','user_id'])['user_id'].count()

should give you what you want

应该给你你想要的

回答by Bernard Esterhuyse

I struggled with the same issue, made use of the solution provided above. You can actually designate any of the columns to count:

我遇到了同样的问题,使用了上面提供的解决方案。您实际上可以指定要计算的任何列:

df.groupby(['revenue','session','user_id'])['revenue'].count()

and

df.groupby(['revenue','session','user_id'])['session'].count()

would give the same answer.

会给出同样的答案。