1

I have a dataset (a long list of values but the form is as shown below) such that | NO | AC_NAME| | :--| :----- | | 0 | X | | 1 | Y |

and a dictionary such that {'X': 'STRING_DATA1', 'Y': 'STRING_DATA2', .....}

How can I add the values of the dictionary as a new column in the top dataset where the AC_NAME matches with the dictionary keys? Please help.

1 Answers1

0

You can use map to map the value from AC_NAME to the dictionary, and assign it to a new column.

d = {'X': 'STRING_DATA1',
 'Y': 'STRING_DATA1'}

df.assign(newColumn=df.AC_NAME.map(d))

   NO AC_NAME     newColumn
0   0       X  STRING_DATA1
1   1       Y  STRING_DATA1
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45