python - 在从 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 有关,因为如果我把它作为一个直接的字典放在代码中它可以工作吗?
谢谢你的帮助!
解决方案
solved. function had same name as dictionary.
推荐阅读
- oracle - 错误:ora-00604&ora-1578 &ora-01110
- python - Python - 创建新列并添加字母
- express - 使用 ES6 import 导入函数会导致 jest 的行为与在同一文件中声明函数不同(表达)
- swift - 应用其他属性时,NSAttributedString 背景颜色不纯色
- visual-studio - 尝试将项目从主要解决方案中拆分出来,并且 Nuget 参考没有看到包
- javascript - ThreeJS 和 PhysiJS “没有‘new’就不能调用类构造函数
- oracle - CICD:如何将 jenkins 用于自动扩展实例池?
- python - Python中的尺度不变OpenCV图像模板匹配
- java - 解析子进程的输出
- python - 如何使用 python 在现有 xlsx 文件中添加 Header 而不会丢失任何数据