python - 基于python中的部分字符串匹配加入数据帧
问题描述
我有两个具有共同关键产品名称的数据框,我想做的是通过基于具有 80-90% 相似性的部分字符串匹配加入前两个数据框来创建第三个数据框,数据集非常大,我曾尝试使用tfidf
from scikit-learn
,但我一直丢失我的参考索引。在下面的示例中:迷你无线蓝牙运动立体声耳机和一加 6 砂岩保护套都需要在 df3 中提供,非常感谢帮助。输出1
例子-
import pandas as pd
df1=pd.DataFrame({'Product_Name1': ['Mini Wireless Bluetooth Sports Stereo Headset', 'VR Box 3D Smart Glass With Remote Controller', 'OnePlus 6 Sandstone Protective Case'],'Price1': [40000, 50000, 42000]})
df2=pd.DataFrame({'Product_Name2': ['Mini Wireless Sports Stereo Headset', 'VR Box 3D Smart Glass With Remote Controller', 'OnePlus 6 1Sandstone Protective Case'], 'Price2': [40000, 50000, 42000]})
df1set=df1.set_index('Product_Name1')
df2set=df2.set_index('Product_Name2')
df3=df1set.join(df2set,how='inner')
df3
df1
df2
解决方案
你需要的是模糊匹配。模糊匹配用于比较彼此非常相似的字符串。你可以用fuzzy wuzzy
这个。
模糊匹配示例
from fuzzywuzzy import process
process.extractOne('Mini Wireless Bluetooth Sports Stereo Headset', df2.Product_Name2)
('Mini Wireless Sports Stereo Headset', 95, 0)
这个值有 95% 的匹配。
我已经更改了 df2 的顺序进行演示。
df1=pd.DataFrame({'Product_Name1': ['Mini Wireless Bluetooth Sports Stereo Headset',
'VR Box 3D Smart Glass With Remote Controller',
'OnePlus 6 Sandstone Protective Case'],
'Price1': [40000, 50000, 42000]})
df1
Product_Name1 Price1
0 Mini Wireless Bluetooth Sports Stereo Headset 40000
1 VR Box 3D Smart Glass With Remote Controller 50000
2 OnePlus 6 Sandstone Protective Case 42000
df2=pd.DataFrame({'Product_Name2': ['Mini Wireless Sports Stereo Headset',
'OnePlus 6 1Sandstone Protective Case',
'VR Box 3D Smart Glass With Remote Controller'],
'Price2': [40000, 42000, 50000]})
df2
Product_Name2 Price2
0 Mini Wireless Sports Stereo Headset 40000
1 OnePlus 6 1Sandstone Protective Case 42000
2 VR Box 3D Smart Glass With Remote Controller 50000
现在我们编写一个函数,将 的每个值df1 Product_Name1
与 的每个值匹配,df2 Product_Name2
并返回 df2 的索引,它与最高匹配。
def fuzzy(x):
closest_match = process.extractOne(x, df2.Product_Name2.values)[0]
index = pd.Index(df2.Product_Name2).get_loc(closest_match)
return index
我们使用apply来得到结果
df1['match'] = df1['Product_Name1'].apply(fuzzy)
df1
Product_Name1 Price1 match
0 Mini Wireless Bluetooth Sports Stereo Headset 40000 0
1 VR Box 3D Smart Glass With Remote Controller 50000 2
2 OnePlus 6 Sandstone Protective Case 42000 1
由于我没有您预期的输出,我将合并它们。
pd.merge(df1, df2, left_on='match', right_on=df2.index)
Product_Name1 Price1 match Product_Name2 Price 2
0 Mini Wireless Bluetooth Sports Stereo Headset 40000 0 Mini Wireless Sports Stereo Headset 40000
1 VR Box 3D Smart Glass With Remote Controller 50000 2 VR Box 3D Smart Glass With Remote Controller 50000
2 OnePlus 6 Sandstone Protective Case 42000 1 OnePlus 6 1Sandstone Protective Case 42000
请让我知道这对你有没有用
推荐阅读
- python - Django我想在数据存在时中断
- kotlin - Quarkus Vert.x 示例
- django - 如何更改 Django 管理员 filter_horizontal 的显示名称
- r - 如何估计 R 中 MARSS 包中的 G 矩阵?
- atk4 - 在包含其他表字段的表单中添加 addCondition()
- flutter - 堆栈内的水平列表视图:水平视口被赋予无限宽度
- java - 如何为列表适配器中的按钮创建 OnClickListener,这将允许我设置 EditText 视图的可见性
- laravel - 尝试 hasFile() 函数时出现错误“调用未定义的方法”
- java - 审核API请求/接口方法的机制/技术
- python - 使用 LDA 模型运行 Sagemaker 批量转换时出错