首页 > 解决方案 > 在从 json 读取的 pandas 列中查找并替换为字典

问题描述

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

specialChars.json {'è':'e', 'µ':'u', 'á':'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 有关,因为如果我把它作为一个直接的字典放在代码中它可以工作吗?

谢谢你的帮助!

标签: pythonpandas

解决方案


solved. function had same name as dictionary.


推荐阅读