假设下pandas
表:
A B C D
1 10 11 12 abc
2 13 14 15 abc
3 16 17 18 def
4 19 20 21 def
5 22 23 24 abc
...
如何用字典中的值替换D列中的元素?例如:
my_dict = { "abc": "test1", "def": "test2", "www": "test3", "aabb": "test4"}
然后该表将如下所示:
A B C D
1 10 11 12 test1
2 13 14 15 test1
3 16 17 18 test2
4 19 20 21 test2
5 22 23 24 test1
...
尝试使用map
:
df['D'] = df['D'].map(my_dict)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句