以下是示例数据框
data = {'A': ['hi UK','hi IN','hi US']}
df = pd.DataFrame(data)
我想从下面的匹配字典更新A列的UK,IN值
abs = {'U': 'UK -- extra', 'UK': 'test Kingdom', 'IN':'India'}
然后我使用了替换功能(pandas.DataFrame.replace)
df['A'] = df['A'].replace(to_replace = abs, regex=True)
print(df)
A
0 hi test Kingdom -- extraK
1 hi India
2 hi test Kingdom -- extraS
它首先用U一次UK -- extra又一次地替换U UK,test kingdom所以最终结果通常是 hi test Kingdom -- extraK应该给测试王国
预期的输出是
A
0 hi test Kingdom
1 hi India
2 hi US
我是否想念任何东西或有什么可以实现上述结果的?
相关分类