使用Python模糊匹配同一数据框中的两列

时间:2018-11-01 14:58:44

标签: python pandas fuzzywuzzy

我在同一数据框中有两个数据集,每个数据集都显示了一个公司列表。一个数据集来自2017年,另一个来自今年。我正在尝试使两个公司的数据集相互匹配,并且认为模糊匹配(FuzzyWuzzy)是做到这一点的最佳方法。使用部分比例,我想简单地列出具有如下所列值的列:去年公司的名称,最高模糊匹配率,今年公司与该最高分数相关联。原始数据帧已被赋予变量“ data”,其去年公司名称位于“公司”列下,而今年公司名称位于“公司名称”列下。为了完成此任务,我尝试使用extractOne模糊匹配过程创建一个函数,然后将该函数应用于数据框中的每个值/行。然后将结果添加到我的原始数据框中。

这是下面的代码:

names_array=[]
ratio_array=[]
def match_names(last_year,this_year):
    for row in last_year:
    x=process.extractOne(row,this_year)
    names_array.append(x[0])
    ratio_array.append(x[1])
return names_array,ratio_array


#last year company names dataset
last_year=data['Company'].dropna().values

#this year companydataset

this_year=data['Company name'].values

name_match,ratio_match=match_names(last_year,this_year)

data['this_year']=pd.Series(name_match)
data['match_rating']=pd.Series(ratio_match)

data.to_csv("test.csv")

但是,每次执行这部分代码时,我创建的两个添加的列都不会显示在csv中。实际上,尽管计算机显示了最近创建的“ test.csv”数据帧,但它与以前的数据帧相同。如果有人可以指出问题或以任何方式帮助我,将不胜感激。

编辑(数据框预览):

          Company                Company name
0                   BODYPHLO  SPORTIQUE                         NaN
1                        JOSEPH A PERRY                         NaN
2                PCH RESORT TENNIS SHOP                         NaN
3              GREYSTONE GOLF CLUB INC.                         NaN
4                 MUSGROVE COUNTRY CLUB                         NaN
5           CITY OF PELHAM RACQUET CLUB                         NaN
6                 NORTHRIVER YACHT CLUB                         NaN
7                           LAKE FOREST                         NaN
8                   TNL TENNIS PRO SHOP                         NaN
9                SOUTHERN ATHLETIC CLUB                         NaN
10           ORANGE BEACH TENNIS CENTER                         NaN

然后在“公司”条目(去年的公司数据集)结束后,“公司名称”列(今年的公司数据集)如下所示:

4168                                NaN                LEWIS TENNIS
4169                                NaN          CHUCKS PRO SHOP AT
4170                                NaN                CHUCK KINYON
4171                                NaN   LAKE COUNTRY RACQUET CLUB
4172                                NaN   SPORTS ACADEMY & RAC CLUB

1 个答案:

答案 0 :(得分:1)

您的数据帧结构很奇怪,考虑到一列仅从另一端开始,但是我们可以使其工作。让我们为您提供的data使用以下示例数据框:

                        Company               Company name
0           BODYPHLO  SPORTIQUE                        NaN
1                JOSEPH A PERRY                        NaN
2        PCH RESORT TENNIS SHOP                        NaN
3      GREYSTONE GOLF CLUB INC.                        NaN
4         MUSGROVE COUNTRY CLUB                        NaN
5   CITY OF PELHAM RACQUET CLUB                        NaN
6         NORTHRIVER YACHT CLUB                        NaN
7                   LAKE FOREST                        NaN
8           TNL TENNIS PRO SHOP                        NaN
9        SOUTHERN ATHLETIC CLUB                        NaN
10   ORANGE BEACH TENNIS CENTER                        NaN
11                          NaN               LEWIS TENNIS
12                          NaN         CHUCKS PRO SHOP AT
13                          NaN               CHUCK KINYON
14                          NaN  LAKE COUNTRY RACQUET CLUB
15                          NaN  SPORTS ACADEMY & RAC CLUB

然后执行您的匹配:

import pandas as pd
from fuzzywuzzy import process, fuzz

known_list = data['Company name'].dropna()

def find_match(x):

    match = process.extractOne(x['Company'], known_list, scorer=fuzz.partial_token_sort_ratio)
    return pd.Series([match[0], match[1]])

data[['this year','match_rating']] = data.dropna(subset=['Company']).apply(find_match, axis=1, result_type='expand')

收益:

                        Company Company name                  this year  \
0           BODYPHLO  SPORTIQUE          NaN  SPORTS ACADEMY & RAC CLUB   
1                JOSEPH A PERRY          NaN         CHUCKS PRO SHOP AT   
2        PCH RESORT TENNIS SHOP          NaN               LEWIS TENNIS   
3      GREYSTONE GOLF CLUB INC.          NaN  LAKE COUNTRY RACQUET CLUB   
4         MUSGROVE COUNTRY CLUB          NaN  LAKE COUNTRY RACQUET CLUB   
5   CITY OF PELHAM RACQUET CLUB          NaN  LAKE COUNTRY RACQUET CLUB   
6         NORTHRIVER YACHT CLUB          NaN  LAKE COUNTRY RACQUET CLUB   
7                   LAKE FOREST          NaN  LAKE COUNTRY RACQUET CLUB   
8           TNL TENNIS PRO SHOP          NaN               LEWIS TENNIS   
9        SOUTHERN ATHLETIC CLUB          NaN  SPORTS ACADEMY & RAC CLUB   
10   ORANGE BEACH TENNIS CENTER          NaN               LEWIS TENNIS   

    match_rating  
0           47.0  
1           43.0  
2           67.0  
3           43.0  
4           67.0  
5           72.0  
6           48.0  
7           64.0  
8           67.0  
9           50.0  
10          67.0