gpt4 book ai didi

python - 将 pandas 一个数据框中的单词替换为其他数据框中的单词

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:05 25 4
gpt4 key购买 nike

我有以下两个数据框

(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**

最佳答案

您可以使用replaceSeries或按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'].to_dict())
print (df)
msg
0 the
1 wi
2 fi
3 account

关于python - 将 pandas 一个数据框中的单词替换为其他数据框中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43228860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com