【发布时间】:2021-06-16 18:49:50
【问题描述】:
我正在尝试使用下面的代码提取句子中的标签,但它会返回关键字。我错过了什么?如何输出以逗号分隔的所有标签(而不是关键字)的新列?
s = set(dict_list)
f = lambda x: ', '.join(set([y for y in x.split() if y in s]))
# df['tags'] = df['description_summary'].apply(f)
df['tags'] = df['description_summary'].apply(lambda x: ', '.join(set(x.split()).intersection(s)))
df
这基本上是我在 excel 文件中使用的数据:
description_summary
0 Long sentence with keywords ball and hot
1 Long sentence with keywords stick, glove, and cold
这是当前(错误的)输出:
description_summary keywords instead of tags
0 Long sentence with keywords ball and hot ball, hot
1 Long sentence with keywords cold, stick, and glove cold, stick, glove
这是我想要的输出:
description_summary tags
0 Long sentence with keywords ball and hot toy, temperature
1 Long sentence with keywords cold, stick, and glove temperature, toy
这里是关键字和标签的字典('keywords': 'tags'):
dict_list = {'Hot': 'Temperature',
'Cold': 'Temperature',
'Very cold': 'Temperature',
'Ball': 'Toy',
'Glove': 'Toy',
'Stick': 'Toy'
}
如何在同一文件的新列中仅输出标签(以逗号分隔)?
【问题讨论】:
-
为什么不继续使用您的标签字典处理您的关键字?
-
什么意思?你能举个例子吗?抱歉,我是新手。
标签: python pandas tags keyword