繁体   English   中英

由另一列 pandas df 分组的值出现的总和

[英]sum of value occurrence grouped by another column pandas df

我需要按列industry统计列name和分组中每个值的出现次数。 目标是获得每个行业的每个名称的总和。 我的数据如下所示:

industry            name
Home             Mike
Home             Mike,Angela,Elliot
Fashion          Angela,Elliot
Fashion          Angela,Elliot

所需的 output 是:

Home Mike:2 Angela:1 Elliot:1
Fashion Angela:2 Elliot:2

将其从评论中移出,经过调试并证明有效:

# count() in the next line won't work without an extra column
df['name_list'] = df['name'].str.split(',')
df.explode('name_list').groupby(['industry', 'name_list']).count()

结果:

                    name
industry name_list      
Fashion  Angela        2
         Elliot        2
Home     Angela        1
         Elliot        1
         Mike          2

您可以使用collections.Counter返回一系列字典,如下所示:

from collections import Counter
s = df.name.str.split(',').groupby(df.industry).sum().agg(Counter)

Out[506]:
industry
Fashion               {'Angela': 2, 'Elliot': 2}
Home       {'Mike': 2, 'Angela': 1, 'Elliot': 1}
Name: name, dtype: object

注意:每个单元格是一个Counter object。 Counter是字典的子类,因此您可以在其上应用字典操作作为字典。

暂无
暂无

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

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