根据python中的部分字符串匹配加入数据框

时间:2018-12-19 07:36:46

标签: python pandas nlp nltk

我有两个具有相同关键产品名称的数据框,我想做的是通过基于具有80-90%相似度的部分字符串匹配来加入前两个来创建第三个数据框,数据集非常大,尝试使用tfidf中的scikit-learn,但是我一直丢失参考索引。在下面的示例中:df3中都需要配备迷你无线蓝牙运动立体声耳机和​​OnePlus 6砂岩保护套,非常感谢您的帮助。输出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

First dataframe

Second dataframe

1 个答案:

答案 0 :(得分:0)

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

让我知道它是否对您有用

相关问题