Skip to content
Advertisement

How to create a nested dictionary from pandas dataframe?

I have the following pandas dataframe that has thousands of rows:

import pandas
...
print(df)

   FAVORITE_FOOD   FAVORITE_DRINK  ...     USER_A        USER_B
0       hamburgers      cola    ...          John          John
1       pasta       lemonade    ...          John          John
2       omelette      coffee    ...          John          John
3       hotdogs         beer    ...          Marie         Marie
4       pizza           wine    ...          Marie         Marie
7       popcorn           oj    ...          Adam          Adam
8       sushi         sprite    ...          Adam          Adam
...
...

I want to create a nested dictionary where people’s names are the keys and the dictionary of their food/drink combination is the value.

Something like this:

dict = {John : {hamburgers : cola, pasta : lemonade, omelette : coffee},
        Marie : {hotdogs : beer, pizza : wine},
        Adam : {popcorn : oj, sushi : sprite} 
            }

Advertisement

Answer

I solved this problem with the following code:

import pandas as pd

# this line groups user ID with their favorite food and drink
group_dict = {k: f.groupby('FAVORITE_FOOD')['FAVORITE_DRINK'].apply(list).to_dict() for k, f in df.groupby('USER_A')}

# then we use dictionary comprehension to create the desired nested dictionary
nested_dict = {outer_k: {inner_k : {inner_v for inner_v in v if inner_k != inner_v} for inner_k, v in outer_v.items()} for outer_k, outer_v in group_dict.items()}

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement