将 Pandas 一个数据框中的单词替换为其他数据框中的单词



>我有两个数据帧

(Pdb) df
                msg
0               the
1               wi
2               fi
3              **a/c**
(Pdb) dictionary
           old_msg    new_msg
0              a/c    account 
1             extend  extend
2             bal     balance

我希望第一个数据帧如下所示(用第二个数据帧替换单词(

(Pdb) df
          msg
0              the
1               wi
2               fi
3              **account**

您可以通过Seriesdict使用replace

df['msg'] = df['msg'].replace(dictionary.set_index('old_msg')['new_msg'])
print (df)
       msg
0      the
1       wi
2       fi
3  account

或:

df['msg'] = df['msg'].replace(dictionary.set_index('old_msg')['new_msg'].to_dict())
print (df)
       msg
0      the
1       wi
2       fi
3  account

最新更新