使用系列查找表替换 Pandas DataFrame 列中的值

Bil*_*ill 6 python dataframe pandas

我想用我准备的系列形式的查找表生成的一组更准确/完整的值替换 DataFrame 中的一列值。

我以为我可以这样做,但结果并不如预期。

这是我要修复的数据帧:

In [6]: df_normalised.head(10)
Out[6]: 
  code                                          name
0    8                             Human development
1   11                                              
2    1                           Economic management
3    6         Social protection and risk management
4    5                         Trade and integration
5    2                      Public sector governance
6   11  Environment and natural resources management
7    6         Social protection and risk management
8    7                   Social dev/gender/inclusion
9    7                   Social dev/gender/inclusion
Run Code Online (Sandbox Code Playgroud)

(注意第 2 行中缺少的名称)。

这是我创建的用于修复的查找表:

In [20]: names
Out[20]: 
1                              Economic management
10                               Rural development
11    Environment and natural resources management
2                         Public sector governance
3                                      Rule of law
4         Financial and private sector development
5                            Trade and integration
6            Social protection and risk management
7                      Social dev/gender/inclusion
8                                Human development
9                                Urban development
dtype: object
Run Code Online (Sandbox Code Playgroud)

这是我认为可以做到的方式:

In [21]: names[df_normalised.head(10).code]
Out[21]: 
code
8                                Human development
11    Environment and natural resources management
1                              Economic management
6            Social protection and risk management
5                            Trade and integration
2                         Public sector governance
11    Environment and natural resources management
6            Social protection and risk management
7                      Social dev/gender/inclusion
7                      Social dev/gender/inclusion
dtype: object
Run Code Online (Sandbox Code Playgroud)

但是,我希望上面的结果系列与 df_normalised 的索引(即 0、1、2、3)具有相同的索引,而不是基于代码值的索引。

所以我不确定如何用这些系列值替换 df_normalised 中“name”列中的原始值,因为索引不一样。

顺便说一句,如何有一个具有上述重复值的索引?

Max*_*axU 6

您可以使用map()函数:

In [38]: df_normalised['name'] = df_normalised['code'].map(name)

In [39]: df_normalised
Out[39]:
   code                                          name
0     8                             Human development
1    11  Environment and natural resources management
2     1                           Economic management
3     6         Social protection and risk management
4     5                         Trade and integration
5     2                      Public sector governance
6    11  Environment and natural resources management
7     6         Social protection and risk management
8     7                   Social dev/gender/inclusion
9     7                   Social dev/gender/inclusion
Run Code Online (Sandbox Code Playgroud)