pandas - 将嵌套字典值映射到dataframe列

时间:2020-12-16 18:12:47

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

我将进一步讨论将字典值映射到数据帧的上一个问题。我有一个简单的数据帧df,如:

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'.

只考虑最常见的L值,即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:

在一个简单的字典案例中,我可以使用df ['C'] = df ['U']。map(d)。我怎样才能只采用以前的最高值?生成的数据框将显示为:

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

1 个解决方案

#1


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

我将dict压平以创建一个新的dict然后你可以像以前一样调用map:

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

#1


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

我将dict压平以创建一个新的dict然后你可以像以前一样调用map:

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