Reputation: 82
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}
}
Upvotes: 1
Views: 338
Reputation: 82
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()}
Upvotes: 2
Reputation: 1144
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.
Upvotes: 0