首页 > 解决方案 > 基于python中的部分字符串匹配加入数据帧

问题描述

我有两个具有共同关键产品名称的数据框,我想做的是通过基于具有 80-90% 相似性的部分字符串匹配加入前两个数据框来创建第三个数据框,数据集非常大,我曾尝试使用tfidffrom 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

第一个数据框

第二个数据框

标签: pythonpandasnlpnltk

解决方案


你需要的是模糊匹配。模糊匹配用于比较彼此非常相似的字符串。你可以用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

请让我知道这对你有没有用


推荐阅读