【发布时间】:2019-01-28 05:36:57
【问题描述】:
这是我的数据集
No Description
1 Paying Google ads
2 Purchasing Facebook Ads
3 Purchasing Ads
4 AirBnB repayment
我有 txt 文件名为 entity.txt
0, Google
1, Facebook
2, Ads
我需要在我的数据框中检测entity.txt 上的所有关键字,无论是单个关键字还是多个关键字,如果没有检测到一个关键字,我们称之为Other,所以我的输出期望是:
No Description Keyword
1 Paying Google ads Google
2 Purchasing Facebook Ads Facebook Ads
3 Purchasing LinkedIn Ads LinkedIn Ads
4 AirBnB repayment Other
这就是我所做的
with open('entity.txt') as f:
content = f.readlines()
content = [x.strip() for x in content ]
df['keyword'] = df['description'].apply(lambda x: ' '.join([i for i in content if i in x]))
df['keyword'] = df['keyword'].replace('', 'Other')
但是,结果是
No Description Keyword
1 Paying Google ads Other
2 Purchasing Facebook Ads Other
3 Purchasing LinkedIn Ads Other
4 AirBnB repayment Other
【问题讨论】: