Python 将一个数据帧中的字替换为另一个数据帧中的字
我有以下两个数据帧Python 将一个数据帧中的字替换为另一个数据帧中的字,python,pandas,dataframe,Python,Pandas,Dataframe,我有以下两个数据帧 (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
(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
我希望第一个数据帧变成如下所示(用第二个数据帧替换单词)
您可以通过系列
或dict
使用:
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'])
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