2

Assume the following pandas table:

   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
...

How can I replace the elements in the column D with values from a dictionary? For example:

my_dict = { "abc": "test1", "def": "test2", "www": "test3", "aabb": "test4"}

Then the table will look like this:

   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
...
Mark
  • 5,372
  • 6
  • 46
  • 103

1 Answers1

2

Try using map:

df['D'] = df['D'].map(my_dict)
U12-Forward
  • 65,118
  • 12
  • 70
  • 89