【问题标题】:fuzzy wuzzy to find a match and other columns associated with match模糊 wuzzy 查找匹配项和与匹配项关联的其他列
【发布时间】:2021-09-29 09:52:14
【问题描述】:

我有一个数据集,我想在地址上进行匹配,然后一旦我有了地址匹配,我还想知道与之关联的相关唯一 ID。

考虑这个例子:

df1 =

Address
123 road
abc lane
1 circle
7th avenue 
4 high street


df2=
Address        unique id
123 rd          ID12345
abc lane        ID12346
1 circle        ID12347
7th ave         ID12348
4 high st       ID12349

df3 (result) =
Address           match       unique id
123 road        123 rd           ID12345
abc lane        abc lane         ID12346
1 circle        1 circle         ID12347
7th avenue      7th ave          ID12348
4 high street   4 high st        ID12349

到目前为止,我已经得到了这个代码:

address_list = df1['Address'].to_list()
address_search = df2['Address'].to_list()

mat1 = []
for i in address_search:
    mat1.append(process.extract(i,address_list, limit=2))
df1['matches'] = mat1

我能够找到匹配项并将其附加到数据框,但我不确定如何搜索与找到的匹配项关联的相关唯一 id 列/行。

【问题讨论】:

    标签: python pandas dataframe fuzzywuzzy


    【解决方案1】:
    • 更新 2。 匹配数据框 #2 行中的任何单词。

      str.contains 接受正则表达式。 因此,您可以用| (or) 符号替换空格,并在一行中查找任何匹配的单词。

      模式如下所示:123|road7th|avenue 等。请注意,这是一个区分大小写的搜索。:

      df2['Address2']=df1['Address'].apply(lambda x: x if df2['Address'].str.contains(x.replace(' ','|')).any() else np.nan)
      

    输出:

    【讨论】:

    • 嘿,我收到一条错误消息:错误:位置 1 的括号不平衡
    • 尝试重新检查括号。或者重新复制代码。在我的电脑上它运行良好,并给出了我附加的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多