Create dataframe that maps a dictionary using string in another column

  dataframe, dictionary, pandas, python

Using a dictionary (example below), need to create a new dataframe column that will example a different column in the dataframe, and whichever first word in the string matches the dictionary key, assign the value to the new column. Example below:

dictionary = {‘dog’:’yellow’, ‘cat’:’black, ‘frog’:’green’, ‘horse’:’brown’}

Original DF:

 ColA:
 The dog and horse ate food
 Where is the frog?
 horse and cat and frog walked together

Desired DF:

 ColA:                                      ColB
 The dog and horse ate food               yellow
 Where is the frog?                       green
 horse and cat and frog walked together   brown

Any suggestions? Thanks!

Source: Python Questions

LEAVE A COMMENT