Create a dictionary from groupby object,Python

You can create a dictionary using a dictionary comprehension as below

df = pd.DataFrame({'Type' : ['Pokemon', 'Pokemon', 'Bird', 'Pokemon', 'Bird', 'Pokemon', 'Pokemon', 'Bird'],'Name' : ['Jerry', 'Jerry', 'Flappy Bird', 'Mudkip','Pigeon', 'Mudkip', 'Jerry', 'Pigeon']})  
f = df.groupby(['Type','Name'])['Type'].agg({'Frequency':'count'})
f.sort('Frequency',ascending=False, inplace=True)

d = {k:list(f.ix[k].index) for k in f.index.levels[0]}
print(d)
# {'Bird': ['Pigeon', 'Flappy Bird'], 'Pokemon': ['Jerry', 'Mudkip']}

The dictionary comprehension will iterate through the outer index ('Bird', 'Pokemon') and then set the value as the inner index for your dictionary.

It is necessary to first sort your MultiIndex by the Frequency column to get the ordering you wish.


Here's a one-liner.

df.groupby(['Type'])['Name'].apply(lambda grp: list(grp.value_counts().index)).to_dict()

# output
#{'Bird': ['Pigeon', 'Flappy Bird'], 'Pokemon': ['Jerry', 'Mudkip']}

The value_counts function implicitly groups the Name field by count and returns descending order by default.

Bonus: if you want to include counts, you can do the following.

df.groupby(['Type']).apply(lambda grp: grp.groupby('Name')['Type'].count().to_dict()).to_dict()

# {'Bird': {'Flappy Bird': 1, 'Pigeon': 2}, 'Pokemon': {'Jerry': 3, 'Mudkip': 2}}