python - 在 2 个数据帧的每一行中查找常用词(交集)
问题描述
我想比较匹配的词(交集)从Dataframe1['Final_Text']
到Dataframe2['Text']
. 第 1 行Dataframe2['Final_Text']
应与 的每一行进行比较Dataframe1['Text']
,类似地,第 2 行Dataframe2['Final_Text']
与 的每一行进行比较Dataframe1['Text']
。
请提出可能的方法。
到现在为止,我已经做了一排
lexicon = set().union(*df2['Final_Text'].str.split())
输出-->
{'study', 'cell' , 'response', 'patient, 'effect','activity' 'cell,', 'protein,', 'result,'}
虚拟数据
data={'activity', 'cell','response','Study','Maths', 'DNA'}
c=data.intersection(lexicon)
print(c)
最终输出--->'cell'
在这里,data
我不想检查Dataframe2['Text']
.
解决方案
您可以使用以下方法遍历数据帧的每一行DataFrame.iterrows
:请参阅此处的文档。这将产生行索引和行本身的内容。这允许您执行以下操作:
intersections = dict()
for index2, row2 in Dataframe2.iterrows():
for index1, row1 in Dataframe1.iterrows():
words1 = set(str(row1[1]).split())
words2 = set(str(row2[1]).split())
matching_words = list(words2.intersection(words1))
rows = 'DF1:{} DF2:{}'.format(index1, index2)
intersections[rows] = matching_words
print(intersections)
>> {'DF1:0 DF2:0': [], 'DF1:1 DF2:0': [… ...}
这将创建一个字典,其中包含两个行索引的字符串是键,相应的交叉点是值,存储和组织所有输出以供进一步使用。
推荐阅读
- cmake - 如何在本地构建、安装和提供 CMake 包?
- c# - 如何读取 csv 文件并将其放入变量中以与 C# 中的给定值进行比较
- java - 从 Adapter 类访问 HashMap 数据
- javascript - 在 key 的动作被触发之前检测 contenteditable div 上的退格事件
- css - CSS:使Overflow-Y中的绝对项目始终填充滚动高度的100%
- c++ - “for(std::vector) 之间的奇怪区别
::iterator it = my_vec.begin(); 它!= my_vec.end(); ++it)" 和 "for(int i:my_vec)" - android - 从另一个屏幕返回后 Recyclerview 刷新 - Android 导航
- sql - 如何分隔/列出 varchar(max) 列中提到的单词
- laravel - Laravel 中基于 API 的观察者
- azure - 通过 REST API 处理 Azure AD MFA