【问题标题】:Rapidfuzz match mergeRapidfuzz 匹配合并
【发布时间】:2021-01-29 07:21:36
【问题描述】:

对此非常陌生,希望对以下内容提供任何建议:

我有一个数据集“项目”,显示具有项目 ID 的机构列表:

project_id  institution_name
0           somali national university
1           aarhus university
2           bath spa
3           aa school of architecture
4           actionaid uk

我想将其与以下“大学”数据集及其国家代码进行模糊匹配合并:

institution_name                      country_code
a tan kapuja buddhista foiskola             HU
aa school of architecture                   UK
bath spa university                         UK
aalto-yliopisto                             FI
aarhus universitet                          DK

然后取回这个:

project_id  institution_name           Match    organisation               country_code
0           somali national university []       NaN                        NaN
1           aarhus university          [(91)]   aarhus universitet         DK
2           bath spa                   [(90)]   bath spa university        UK
3           aa school of architecture  [(100)]  aa school of architecture  UK
4           actionaid uk               []       NaN                        NaN

使用快速模糊:

import pandas as pd

import numpy as np

from rapidfuzz import process, utils as fuzz_utils

def fuzzy_merge(baseFrame, compareFrame, baseKey, compareKey, threshold=90, limit=1, how='left'):
    #   baseFrame: the left table to join
    #   compareFrame: the right table to join
    #   baseKey: key column of the left table
    #   compareKey: key column of the right table
    #   threshold: how close the matches should be to return a match, based on Levenshtein distance
    #   limit: the amount of matches that will get returned, these are sorted high to low
    #   return: dataframe with boths keys and matches
    s_mapping = {x: fuzz_utils.default_process(x) for x in compareFrame[compareKey]}

    m1 = baseFrame[baseKey].apply(lambda x: process.extract(
      fuzz_utils.default_process(x), s_mapping, limit=limit, score_cutoff=threshold, processor=None
    ))
    baseFrame['Match'] = m1

    m2 = baseFrame['Match'].apply(lambda x: ', '.join(i[2] for i in x))
    baseFrame['organisation'] = m2

    return baseFrame.merge(compareFrame, on=baseKey, how=how)

Merged = fuzzy_merge(Projects, Universities, 'institution_name', 'institution_name')

Merged

我得到了这个(匹配列中有一些额外的文本,但现在不会进入)。这几乎是我想要的,但国家代码只有在 100% 匹配时才匹配:

project_id  institution_name           Match    organisation               country_code
0           somali national university []       NaN                        NaN
1           aarhus university          [(91)]   aarhus universitet         NaN
2           bath spa                   [(90)]   bath spa university        NaN
3           aa school of architecture  [(100)]  aa school of architecture  UK
4           actionaid uk               []       NaN                        NaN

我认为这是我如何将我的 basekey 与 compareframe 进行比较以创建我的合并数据集的问题。不过,我无法弄清楚如何在“组织”上返回它 - 尝试将其插入会导致各种错误。

【问题讨论】:

    标签: python pandas rapidfuzz


    【解决方案1】:

    没关系,想通了 - 我没有考虑空单元格!用 NaN 替换它们效果很好。

    def fuzzy_merge(baseFrame, compareFrame, baseKey, compareKey, threshold=90, limit=1, how='left'):
        s_mapping = {x: fuzz_utils.default_process(x) for x in compareFrame[compareKey]}
    
        m1 = baseFrame[baseKey].apply(lambda x: process.extract(
          fuzz_utils.default_process(x), s_mapping, limit=limit, score_cutoff=threshold, processor=None
        ))
        baseFrame['Match'] = m1
    
        m2 = baseFrame['Match'].apply(lambda x: ', '.join(i[2] for i in x))
        baseFrame['organisations'] = m2.replace("",np.nan)
    
        return baseFrame.merge(compareFrame, left_on='organisations', right_on=compareKey, how=how)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 2016-08-05
      • 2012-08-09
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多