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

问题描述 投票:0回答:1

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

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有关,因为如果我将它作为直接字典放入代码中,它会起作用吗?

感谢您的帮助!

python pandas
1个回答
0
投票

已解决。函数的名称与字典相同。

© www.soinside.com 2019 - 2024. All rights reserved.