1

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} 
            }
1
  • you can do this with this code: def apply_build_dic(r): if r['USER_A'] not in res: res[r['USER_A']] = {} res[r['USER_A']][r['FAVORITE_FOOD']] = r['FAVORITE_DRINK'] res = {} df.apply(apply_build_dic, axis=1) NOTE: it is not clear what to use USER_A or USER_B. Please, clarify it. Commented Apr 23, 2021 at 22:39

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

thanks for your answer! this works but the runtime was super long for my large data frame. I found a solution that is much faster

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.