使用字典替换pandas列中字符串中的字符串

使用字典替换pandas列中字符串中的字符串,第1张

使用字典替换pandas列中字符串中的字符串

您可以创建

dictionary
然后
replace

ids = {'Id':['NYC','LA','UK'],      'City':['New York City','Los Angeles','United Kingdom']}ids = dict(zip(ids['Id'], ids['City']))print (ids){'UK': 'United Kingdom', 'LA': 'Los Angeles', 'NYC': 'New York City'}df['commentTest'] = df['Comment'].replace(ids, regex=True)print (df)  Categories Comment  Type       animal      The NYC tree is very big  tree   1      plant  The cat from the UK is small   dog   2     object     The rock was found in LA.  rock          commentTest  0        The New York City tree is very big  1  The cat from the United Kingdom is small  2        The rock was found in Los Angeles.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5674599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存