Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dictionary from groupby object,Python

Suppose 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 group it according to the type:

print df.groupby(['Type','Name'])['Type'].agg({'Frequency':'count'})

                           Frequency
Type    Name                  
Bird    Flappy Bird          1
        Pigeon               2
Pokemon Jerry                3
        Mudkip               2

Could i create a dictionary from the above group ?? The key "Bird" will have a value of list containing ['Pigeon',Flappy Bird'] note that higher frequency name should appear first in the Value list.

Expected Output:

dict1 = { 'Bird':['Pigeon','Flappy Bird'] , 'Pokemon':['Jerry','Mudkip'] }
like image 745
Hypothetical Ninja Avatar asked May 05 '14 10:05

Hypothetical Ninja


People also ask

How do I turn a Groupby object into a list?

You can group DataFrame rows into a list by using pandas. DataFrame. groupby() function on the column of interest, select the column you want as a list from group and then use Series. apply(list) to get the list for every group.

What can you do with a Groupby object?

A groupby operation involves some combination of splitting the object, applying a function, and combining the results. This can be used to group large amounts of data and compute operations on these groups.

What does DF Groupby ([ genre ]) do?

groupby() function is used to split the data into groups based on some criteria. pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names.

What is possible using Groupby () method of pandas?

groupby() can accept several different arguments: A column or list of columns. A dict or pandas Series. A NumPy array or pandas Index , or an array-like iterable of these.


1 Answers

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.

like image 178
Ffisegydd Avatar answered Sep 18 '22 15:09

Ffisegydd