【发布时间】:2018-04-17 14:43:49
【问题描述】:
我有一个熊猫数据框包含两列:-
ID text_data
1 companies are mainly working on two
technologies that is ai and health care.
Company need to improve on health care.
2 Current trend are mainly depends on block chain
and IOT where IOT is
highly used.
3 ............
. ...........
. ...........
. so on.
现在我有另一个列表 Techlist=["block chain","health care","ai","IOT"]
我需要将列表Techlist 与熊猫数据框的text_data 列匹配,所以我使用了以下代码:-
df['tech_match']=df['text_data'].apply(lambda x: [reduce(op.add, re.findall(act,x)) for act in Techlist if re.findall(act,x) <> []] )
所以我得到的是不同的东西:-
ID text_data tech_match
1 companies are mainly working on two [ai,healthcarehealthcare]
technologies that is ai and health care.
Company need to improve on health care.
2 current trend are mainly [block chain,IOTIOT]
depends on block chain and
IOT where IOT is highly used.
3 .................
. ................
. ...............
. so on.
列表和文本数据已正确匹配,但匹配的列表词在tech_match 列中重复。
我需要的是:-
ID text_data tech_match
1 companies are mainly working on two [heatlh care,ai]
technologies that is ai and health care.
Company need to improve on health care.
2 Current trend are mainly depends on [block chain,IOT]
blockchain and IOT where IOT is
highly used.
3 ..................
. ..................
. .................
. son on.
如何删除tech_match 列中的这些重复词?
【问题讨论】:
-
使用
set() -
天哪,这是您正在应用的低效功能。首先,不要使用
reduce(op.add, re.findall(act,x)),你应该''.join字符串,而不是+一起使用,一个是O(n),一个是O(n^2)。此外,您调用re.findall(act,x)两次,只需编写一个完整的函数,然后停止尝试将所有内容都放入一个单行中!
标签: python pandas text-mining