假设我有一个pandas数据帧,如下所示:
ID String1 String2
1 The big black wolf The small wolf
2 Close the door on way out door the Close
3 where's the money where is the money
4 123 further out out further
在进行模糊字符串匹配之前,我想交叉标记String1和String2列中的每一行,类似于Python模糊字符串匹配作为相关性样式表/矩阵。
我面临的挑战是,我发布的链接中的解决方案只有在String1和String2中的字数相同时才有效。其次,该解决方案会查看列中的所有行,而我希望我的解决方案只进行逐行比较。
建议的解决方案应该对第1行进行类似矩阵的比较,如:
string1 The big black wolf Maximum
string2
The 100 0 0 0 100
small 0 0 0 0 0
wolf 0 0 0 100 100
ID String1 String2 Matching_Average
1 The big black wolf The small wolf 66.67
2 Close the door on way out door the Close
3 where's the money where is the money
4 123 further out out further
其中匹配平均值是"最大"列的总和除以String2 中的字数
您可以首先从2个系列中获得虚设,然后获得列的交集,将它们相加并除以第二列的虚设:
a = df['String1'].str.get_dummies(' ')
b = df['String2'].str.get_dummies(' ')
u = b[b.columns.intersection(a.columns)]
df['Matching_Average'] = u.sum(1).div(b.sum(1)).mul(100).round(2)
print(df)
ID String1 String2 Matching_Average
0 1 The big black wolf The small wolf 66.67
1 2 Close the door on way out door the Close 100.00
2 3 where's the money where is the money 50.00
3 4 123 further out out further 100.00
否则,如果您可以使用字符串匹配算法,则可以使用difflib
:
from difflib import SequenceMatcher
[SequenceMatcher(None,x,y).ratio() for x,y in zip(df['String1'],df['String2'])]
#[0.625, 0.2564102564102564, 0.9142857142857143, 0.6153846153846154]