Fabio Lamanna
Fabio Lamanna

Reputation: 21584

pandas - map nested dictionary values to dataframe column

I'm going a little further this previous question about mapping dictionary values to dataframes. I have a simple dataframe df like:

U,id
111,01
112,02
112,03
113,04
113,05
113,06
114,07

and I want to map on a new column the following nested dictionary:

d = {112: {'en': 1, 'es': 2}, 113: {'zh': 1, 'ja': 1, 'es': 2}, 114: {'es': 1}, 111: {'ar': 2, 'en': 1}}

taking into account only the most frequent L values, i.e. 112:'es', 113:'es', 114:'es', 111:'ar'.

On a simple dictionary case, I can use df['C'] = df['U'].map(d). How can I do the same taking only the previous highest values? The resulting dataframe would appear as:

U,id,C
111,01,ar
112,02,es
112,03,es
113,04,es
113,05,es
113,06,es
114,07,es

Upvotes: 5

Views: 5984

Answers (1)

EdChum
EdChum

Reputation: 394399

I'd flatten the dict to create a new dict and then you can call map as before:

In [44]:

max_d={}
for k,v in d.items():
    max_d[k] = max(v, key=v.get)
max_d
Out[44]:
{111: 'ar', 112: 'es', 113: 'es', 114: 'es'}
In [45]:

df['C'] = df['U'].map(max_d)
df  
Out[45]:
     U  id   C
0  111   1  ar
1  112   2  es
2  112   3  es
3  113   4  es
4  113   5  es
5  113   6  es
6  114   7  es

Upvotes: 6

Related Questions