分组和计算频率, pandas [英] Grouping and Computing Frequency ,Pandas
本文介绍了分组和计算频率, pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框:
df = pd.DataFrame({'Type' : ['Pokemon', 'Pokemon', 'Bird', 'Pokemon', 'Bird', 'Pokemon', 'Pokemon', 'Bird'],'Name' : ['Jerry', 'Jerry', 'Flappy Bird', 'Mudkip','Pigeon', 'Mudkip', 'Jerry', 'Pigeon']})
,我需要将观察结果归类,即所有神奇宝贝类型及其各自的名称.而且我需要添加另一列,该列具有类型中名称出现的频率.看起来应该像:
and i need to group the observations w.r.t their types i.e all pokemon types together with their respective names . And i need to add another column which has the frequency of occurrence of the names in the types. It should look like :
Type Name Frequency
Pokemon Jerry 3
Mudkip 2
Bird Pigeon 2
Flappy Bird 1
我用过:
data2 = df.groupby(['Type'])
但这并没有按照需要的方式进行分组.
请帮忙.
but that doesn't group it the way it needs to be.
Please help.
推荐答案
我认为您想同时对类型"和名称"进行分组:
I think you want to group on both 'Type' and 'Name':
print df.groupby(['Type','Name']).size()
Type Name
Bird Flappy Bird 1
Pigeon 2
Pokemon Jerry 3
Mudkip 2
或者如果重要的是要有名为"Frequency"的列,则可以执行以下操作:
Or if it is important to have the column named 'Frequency', you could do something like the following:
print df.groupby(['Type','Name'])['Type'].agg({'Frequency':'count'})
Frequency
Type Name
Bird Flappy Bird 1
Pigeon 2
Pokemon Jerry 3
Mudkip 2
这篇关于分组和计算频率, pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文