我有以下数据帧
Column1 Column2
tomato fruit tomatoes are not a fruit
potato la best potatoe are some sort of fruit
apple there are great benefits to appel
pear peer我已经使用了两种不同的方法:
for i in range(0, len(Column1)):
store_it = SM(None, Column1[i], Column2[i]).get_matching_blocks()
print(store_it)和
df['diff'] = df.apply(lambda x: diff.SequenceMatcher(None, x[0].strip(), x[1].strip()).ratio(), axis=1) 我在网上找到的。
第二种方法工作得很好,只是它会尝试匹配整个短语。我如何才能将第一列中的单词与第二列中的句子进行匹配,以便最终给出'Yes‘它们在句子中(或部分)或'No’它们不在句子中。
发布于 2018-10-05 04:08:54
在这一点上,我使用FuzzyWuzzy的部分比率取得了最好的成功。它将给出Column1“番茄水果”和Column2“西红柿不是水果”以及列中其余部分之间的部分%匹配比率。查看结果:
from fuzzywuzzy import fuzz
import difflib
df['fuzz_partial_ratio'] = df.apply(lambda x: fuzz.partial_ratio(x['Column1'], x['Column2']), axis=1)
df['sequence_ratio'] = df.apply(lambda x: difflib.SequenceMatcher(None, x['Column1'], x['Column2']).ratio(), axis=1)你可以认为任何FuzzyWuzzy分数> 60都是很好的部分匹配,也就是说,是的,Column1中的单词最有可能出现在Column2中的句子中。
第1行-分数67,第2行-分数71,第3行-分数80,第4行-分数75
发布于 2017-06-03 16:52:56
使用set():
issubset(other)
设置<= other
测试集合中的每个元素是否都在其他元素中。
例如:
c_set1 = set(Column1[i])
c_set2 = set(Column2[i])
if c_set1.issubset(c_set2):
# every in c_set1 is in c_set2https://stackoverflow.com/questions/44313964
复制相似问题