I have the following list:
search_list = ['STEEL','IRON','GOLD','SILVER']
which I need to search within a dataframe (df):
a b
0 123 'Blah Blah Steel'
1 456 'Blah Blah Blah'
2 789 'Blah Blah Gold'
and insert the matching rows into a new dataframe (newdf), adding a new column with the matching word from the list:
a b c
0 123 'Blah Blah Steel' 'STEEL'
1 789 'Blah Blah Gold' 'GOLD'
I can use the following code to extract the matching row:
newdf=df[df['b'].str.upper().str.contains('|'.join(search_list),na=False)]
but I can't figure out how to add the matching word from the list into column c.
I'm thinking that the match somehow needs to capture the index of the matching word in the list and then pull the value using the index number but I can't figure out how to do this.
Any help or pointers would be greatly appreciated
Thanks