I have the following pandas dataframe that has thousands of rows:
JavaScript
x
15
15
1
import pandas
2
3
print(df)
4
5
FAVORITE_FOOD FAVORITE_DRINK USER_A USER_B
6
0 hamburgers cola John John
7
1 pasta lemonade John John
8
2 omelette coffee John John
9
3 hotdogs beer Marie Marie
10
4 pizza wine Marie Marie
11
7 popcorn oj Adam Adam
12
8 sushi sprite Adam Adam
13
14
15
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:
JavaScript
1
5
1
dict = {John : {hamburgers : cola, pasta : lemonade, omelette : coffee},
2
Marie : {hotdogs : beer, pizza : wine},
3
Adam : {popcorn : oj, sushi : sprite}
4
}
5
Advertisement
Answer
I solved this problem with the following code:
JavaScript
1
9
1
import pandas as pd
2
3
# this line groups user ID with their favorite food and drink
4
group_dict = {k: f.groupby('FAVORITE_FOOD')['FAVORITE_DRINK'].apply(list).to_dict() for k, f in df.groupby('USER_A')}
5
6
# then we use dictionary comprehension to create the desired nested dictionary
7
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()}
8
9