learning_to_code
learning_to_code

Reputation: 31

Use list to find keyword match in Dataframe

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

Answers (1)

drake10k
drake10k

Reputation: 437

Have you tried using findall? Should do the trick with it.

df['word'] = df['survey_response'].str.findall('({})'.format(query))

Upvotes: 2

Related Questions