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
  • 6,052
  • 8
  • 61
  • 129

1 Answers1

2

Try using map:

df['D'] = df['D'].map(my_dict)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114