【发布时间】:2020-02-05 03:36:38
【问题描述】:
我的数据框中有一列“描述”,我正在此列中搜索关键字列表。如果关键字出现在特定行中,我能够返回 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
【问题讨论】:
-
发布你写到这里的代码
标签: python regex pandas dataframe