我的数据框中有一列“Description”,我正在这个列中搜索关键字列表。如果关键字出现在特定行中,我可以返回True或False值。我想再添加一列,显示列表中的哪个关键字与该行中的数据匹配
例如:
content = ['paypal', 'silverline', 'bcg', 'onecap']
#dataframe df
Description Debit Keyword_present
onech xmx paypal 555 True
xxl 1ef yyy 141 False
bcg tte exact 411 True
新列应该如下所示:
Keyword
paypal
NA
bcg
到目前为止,如果关键字存在,我已经尝试获取T/F值
#content is my list of keywords
present = new_df['Description'].str.contains('|'.join(content))
new_df['Keyword Present'] = present
如果
description
中的值总是用空格隔开,那么可以使用它将返回一个set对象,您可以随意修改它
这种方法的一个优点是它可以提供多个匹配字符串
使用
contains
而不是extract
的模式有些不同:输出:
相关问题 更多 >
编程相关推荐