Reputation: 31
trying to loop through a list and row to find a keyword match and create new column 'Word' that stores these matches in it
'angry and sometimes upset' is only returning the first match 'angry' and not 'upset'
How can I get the output to show all keyword matches i.e. ['angry','upset']
any help would be great!
import pandas as pd
df = pd.DataFrame({
'survey_response':[
'mostly happy',
'angry and sometimes upset',
'not sure',
'really happy.',
'a bit sad',
'happy probably very happy',
]
})
search_words = ['happy', 'sad', 'angry','very happy', 'upset']
query = '|'.join(search_words)
df['match'] = df['survey_response'].str.contains(query, case=False)
df['word'] = df['survey_response'].str.extract( '({})'.format(query) )
Upvotes: 0
Views: 360