分组和计算频率,Pandas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23402150/
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
Grouping and Computing Frequency ,Pandas
提问by Hypothetical Ninja
I have a dataframe :
我有一个数据框:
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
I used :
我用了 :
data2 = df.groupby(['Type'])
but that doesn't group it the way it needs to be.
Please help.
但这并没有按照它需要的方式对其进行分组。
请帮忙。
回答by Karl D.
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
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

