使用列表在dataframe中查找关键字匹配

pwuypxnk  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(344)

尝试在列表和行中循环查找关键字匹配项,并创建新列“word”,将这些匹配项存储在其中
“愤怒,有时心烦意乱”只是在第一场比赛中返回“愤怒”,而不是“心烦意乱”
如何获得显示所有关键字匹配的输出,即:[“生气”、“心烦”]
任何帮助都会很好!

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) )
nzk0hqpo

nzk0hqpo1#

你试过用芬德尔吗?你应该用它来耍把戏。

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

相关问题