我想从“意见”列中提取所有匹配的关键字,如果它们与关键字列表中的某个单词匹配,则在新列中打印所有匹配的单词(包括重复的单词)。当前代码只提取第一个匹配的单词,不包括重复的单词
import pandas as pd
df = pd.DataFrame({
'opinions':[
"I think the movie is fantastic. Shame it's so short!",
"How did they make it?",
"I had a fantastic time at the cinema last night!",
"I really disliked the cast",
"the film was sad and boring",
"Absolutely loved the movie! Can't wait to see part 2",
]
})
keywords = ['movie', 'great', 'fantastic', 'loved']
query = '|'.join(keywords)
df['word'] = df['opinions'].str.extract( '({})'.format(query) )
print(df)
电流输出
如果只想匹配完整单词,则需要使用单词边界标记,否则前缀(和后缀)也将匹配。例如:
输出
在上面的示例中
greatness
由于单词边界(\b
)而不匹配关于性能的说明
如果您正在寻找一个有效的大数据解决方案,那么联合正则表达式并不是一个好方法(请参见here)。我建议您使用trrex之类的库
输出(使用trrex)
有关性能的比较,请参见下图:
对于一组25K字,trrex比union正则表达式快300倍。上图中的实验可以用下面的gist复制
免责声明:我是trrex的作者
应将} :
extract
替换为^{相关问题 更多 >
编程相关推荐