在从json读取的列中查找并替换为字典

2024-09-30 01:21:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用从json文件读取的字典替换pandas列中的字符和单词

specialChars.json {'è':'e', ‘µ’:‘u’, 'a':'a', 'bar':'bat'}

with open('specialChars.json', 'r', encoding='utf-8') as handle:
    specialChars = json.loads(handle.read())

df
   col1
0  foo bar
1  fèe foo

desired result
   col1
0  foo bat
1  fee foo

我试过了

df[col1].replace(specialChars, regex=True)

它似乎与读取json有关,因为如果我把它作为一个直接的字典放在代码中,它会工作吗

谢谢你的帮助


Tags: 文件jsonpandasdf字典foowithbar

热门问题