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.
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
来源:https://stackoverflow.com/questions/23402150/grouping-and-computing-frequency-pandas