Home > Mobile >  Create dataframe that maps a dictionary using string in another column
Create dataframe that maps a dictionary using string in another column

Time:10-16

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!

CodePudding user response:

Try:

df["ColB"] = df["ColA"].str.split(expand=True).apply(lambda x: x.str.strip('.?,!":;').map(dictionary)).bfill(axis=1)[0]

                                     ColA    ColB
0              The dog and horse ate food  yellow
1                      Where is the frog?   green
2  horse and cat and frog walked together   brown
  • Related