python模糊匹配算法_是否可以与python pandas进行模糊匹配合并?

使用fuzzywuzzy

2019年答案

由于没有fuzzywuzzy软件包的示例,因此我编写了以下函数,该函数将根据您可以设置为用户的阈值返回所有匹配项:

示例数据框

df1 = pd.DataFrame({'Key':['Apple', 'Banana', 'Orange', 'Strawberry']})

df2 = pd.DataFrame({'Key':['Aple', 'Mango', 'Orag', 'Straw', 'Bannanna', 'Berry']})

# df1

Key

0 Apple

1 Banana

2 Orange

3 Strawberry

# df2

Key

0 Aple

1 Mango

2 Orag

3 Straw

4 Bannanna

5 Berry

模糊匹配功能

def fuzzy_merge(df_1, df_2, key1, key2, threshold=90, limit=2):

"""

df_1 is the left table to join

df_2 is the right table to join

key1 is the key column of the left table

key2 is the key column of the right table

threshold is how close the matches should be to return a match, based on Levenshtein distance

limit is the amount of matches that will get returned, these are sorted high to low

"""

s = df_2[key2].tolist()

m = df_1[key1].apply(lambda x: process.extract(x, s, limit=limit))

df_1['matches'] = m

m2 = df_1['matches'].apply(lambda x: ', '.join([i[0] for i in x if i[1] >= threshold]))

df_1['matches'] = m2

return df_1

在数据帧上使用我们的函数:#1

from fuzzywuzzy import fuzz

from fuzzywuzzy import process

fuzzy_merge(df1, df2, 'Key', 'Key', threshold=80)

Key matches

0 Apple Aple

1 Banana Bannanna

2 Orange Orag

3 Strawberry Straw, Berry

在数据框上使用我们的函数:#2

df1 = pd.DataFrame({'Col1':['Microsoft', 'Google', 'Amazon', 'IBM']})

df2 = pd.DataFrame({'Col2':['Mcrsoft', 'gogle', 'Amason', 'BIM']})

fuzzy_merge(df1, df2, 'Col1', 'Col2', 80)

Col1 matches

0 Microsoft Mcrsoft

1 Google gogle

2 Amazon Amason

3 IBM

安装:

点子

pip install fuzzywuzzy

水蟒

conda install -c conda-forge fuzzywuzzy

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值