首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python-查找DataFrame列(刮过的文本)和字符串列表之间的匹配字符串。

Python-查找DataFrame列(刮过的文本)和字符串列表之间的匹配字符串。
EN

Stack Overflow用户
提问于 2021-10-31 22:06:18
回答 1查看 1.2K关注 0票数 0

我很难将DataFrame列中的字符串与字符串列表进行比较。

让我解释一下:我为一个个人项目从社交媒体收集数据,除此之外,我还创建了一个字符串列表,如下所示:

代码语言:javascript
运行
复制
the_list = ['AI', 'NLP', 'approach', 'AR Cloud', 'Army_Intelligence', 'Artificial general intelligence', 'Artificial tissue', 'artificial_insemination', 'artificial_intelligence', 'augmented intelligence', 'augmented reality', 'authentification', 'automaton', 'Autonomous driving', 'Autonomous vehicles', 'bidirectional brain-machine interfaces', 'Biodegradable', 'biodegradable', 'Biotech', 'biotech', 'biotechnology', 'BMI', 'BMIs', 'body_mass_index', 'bourdon', 'Bradypus_tridactylus', 'cognitive computing', 'commercial UAVs', 'Composite AI', 'connected home', 'conversational systems', 'conversational user interfaces', 'dawdler', 'Decentralized web', 'Deep fakes', 'Deep learning', 'defrayal']

还有其他的话,但这只是给你一个想法。

我的目标是比较这个列表中的每一个单词,与包含标题和帖子消息(来自reddit)的两个现有DF列进行比较。为了清楚起见,我想创建一个新的列,在其中显示我的列表与包含这些帖子的列之间的单词。

到目前为止,这就是我所做的:

代码语言:javascript
运行
复制
the_list = ['AI', 'NLP', 'approach', 'AR Cloud', 'Army_Intelligence', 'Artificial general intelligence', 'Artificial tissue', 'artificial_insemination', 'artificial_intelligence', 'augmented intelligence', 'augmented reality', 'authentification', 'automaton', 'Autonomous driving', 'Autonomous vehicles', 'bidirectional brain-machine interfaces', 'Biodegradable', 'biodegradable', 'Biotech', 'biotech', 'biotechnology', 'BMI', 'BMIs', 'body_mass_index', 'bourdon', 'Bradypus_tridactylus', 'cognitive computing', 'commercial UAVs', 'Composite AI', 'connected home', 'conversational systems', 'conversational user interfaces', 'dawdler', 'Decentralized web', 'Deep fakes', 'Deep learning', 'defrayal']

df['matched text'] = df.text_lemmatized.str.extract('({0})'.format('|'.join(the_list)), flags = re.IGNORECASE)
df = df[~pd.isna(df['matched text'])]

df

>>Outpout:

      title_lemmatized   text_lemmatized        matched_word(s)
0         Title1       'claim thorough vet...'      'ai'
1         Title@       'Yeaaah today iota...'       'IoT'

在这里,输出结果可以获得更多细节。

问题:主要问题是它返回与列表匹配的字母(而不是实际单词)。

示例:

-- the_list = 'ai‘(用于人工智能)或IoT (用于物联网)

- df' text _lemmatized‘在文本中有'claim’这个词,那么'ai‘就是匹配的词。或者“Iota”将与“物联网”相匹配。

我想要的:

代码语言:javascript
运行
复制
   title_lemmatized       text_lemmatized             matched_word(s)
0    Title1         'AI claim that Iot devises...'      'AI', 'IoT'
1    Title2         'The claim story about...'
2    Title3         'augmented reality and ai are...'   'augmented reality', 'ai'
3    Title4         'AI ai or artificial intelligence'  'AI', 'ai', 'artificial intelligence'

谢谢罗得:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-31 23:12:20

您必须将单词边界'\b'添加到正则表达式中。来自再模块文档

\b 匹配空字符串,但仅在单词的开头或结尾匹配。单词被定义为单词字符的序列。注意,在形式上,\b被定义为a \w和a \W字符之间的边界(反之亦然),或者是\w和字符串的开头/结尾之间的边界。这意味着r'\bfoo\b‘匹配' foo’、'foo.‘、'(foo)’、'bar foo baz‘而不是'foobar’或‘foo 3’。

此外,您希望使用Series.str.findall (或Series.str.extractall)而不是Series.str.extract来查找所有匹配项。

这应该能行

代码语言:javascript
运行
复制
the_list = ['AI', 'NLP', 'approach', 'AR Cloud', 'Army_Intelligence', 'Artificial general intelligence', 'Artificial tissue', 'artificial_insemination', 'artificial_intelligence', 'augmented intelligence', 'augmented reality', 'authentification', 'automaton', 'Autonomous driving', 'Autonomous vehicles', 'bidirectional brain-machine interfaces', 'Biodegradable', 'biodegradable', 'Biotech', 'biotech', 'biotechnology', 'BMI', 'BMIs', 'body_mass_index', 'bourdon', 'Bradypus_tridactylus', 'cognitive computing', 'commercial UAVs', 'Composite AI', 'connected home', 'conversational systems', 'conversational user interfaces', 'dawdler', 'Decentralized web', 'Deep fakes', 'Deep learning', 'defrayal']

pat = r'\b({0})\b'.format('|'.join(the_list))
df['matched text'] = df.text_lemmatized.str.findall(pat, flags = re.IGNORECASE).map(", ".join)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69790956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档