首页 > 解决方案 > 两个名称非常相似的列表的模糊匹配

问题描述

我知道这个问题以某种方式被问过,所以很抱歉。我正在尝试将列表 1(样本名称)模糊匹配到列表 2(实际名称)。Actual_name 的名称比列表 1 的名称多得多,而且我一直在运行模糊匹配,但效果不佳。我尝试了多种模糊匹配方法(部分,set_token),但由于列表 2 中有更多非常相似的名称,所以一直遇到问题。有什么办法可以改善这里的匹配。理想情况下,希望有列表 1,列表 2 中的匹配名称,匹配分数在新数据框中的第 3 列中。任何帮助将非常感激。谢谢。

到目前为止已经使用了这个:

df1=sample_df['sample_name'].to_list()
df2=actual_df['actual_name'].to_list()
response = {}
for name_to_find in df1:
   for name_master in df2:
     if fuzz.partial_ratio(name_to_find,name_master) > 90:
       response[name_to_find] = name_master
       break
for key, value in response.item():
  print('sample name' + key + 'actual_name' + value)


样品名称 实际名称
体育 JT体育有限责任公司
汤姆棒球 汤姆棒球公司
上下文表达 上下文快递有限责任公司
zb西西里岛 ZB西西里有限责任公司
闪电快车 闪电快车有限责任公司
消防道路 火路快车
不适用 地球跋涉
不适用 TS体育有限责任公司
不适用 MM棒球公司
不适用 联系快递有限责任公司
不适用 AB西西里有限责任公司
不适用 闪电道路有限责任公司

标签: pythonfuzzy-searchfuzzywuzzy

解决方案


不确定这是否是您的预期输出(您可能需要调整阈值),但我认为这就是您要找的?

import pandas as pd
from fuzzywuzzy import process

threshold = 50

list1 = ['jtsports','tombaseball','context express','zb sicily',
         'lightening express','fire roads']
list2 = ['JT Sports LLC','Tom Baseball Inc.','Context Express LLC',
'ZB Sicily LLC','Lightening Express LLC','Fire Road Express',
'Earth Treks','TS Sports LLC','MM Baseball Inc.','Contact Express LLC',
'AB Sicily LLC','Lightening Roads LLC']


response = []
for name_to_find in list1:
    resp_match =  process.extractOne(name_to_find ,list2)
    if resp_match[1] > threshold:
         row = {'sample_name':name_to_find,'actual_name':resp_match[0], 'score':resp_match[1]}
         response.append(row)
         print(row)

results = pd.DataFrame(response)

# If you need all the 'actual_name' tp be in the datframe, continue below
# Otherwise don't include these last 2 lines of code
unmatched = pd.DataFrame([x for x in list2 if x not in list(results['actual_name'])], columns=['actual_name'])
results = results.append(unmatched, sort=False).reset_index(drop=True)

输出:

print(results)
           sample_name             actual_name  score
0             jtsports           JT Sports LLC   79.0
1          tombaseball       Tom Baseball Inc.   81.0
2      context express     Context Express LLC   95.0
3            zb sicily           ZB Sicily LLC   95.0
4   lightening express  Lightening Express LLC   95.0
5           fire roads       Fire Road Express   86.0
6                  NaN             Earth Treks    NaN
7                  NaN           TS Sports LLC    NaN
8                  NaN        MM Baseball Inc.    NaN
9                  NaN     Contact Express LLC    NaN
10                 NaN           AB Sicily LLC    NaN
11                 NaN    Lightening Roads LLC    NaN

推荐阅读