Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将一个数据帧中的字替换为另一个数据帧中的字_Python_Pandas_Dataframe - Fatal编程技术网

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