简体   繁体   中英

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} 
            }

You can create desired dictionary by

dict1 = {}

for i in range(len(df)):
  row = df.iloc[i, :]
  dict1.setdefault(row["USER_A"],{}).update({row["FAVORITE_FOOD"] : row["FAVORITE_DRINK"]})

I used setdefault method to initially create empty dictionary and then append other dictionary as a value.

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()}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM