在Col2中查找Col1的项目并评论匹配的百分比

问题描述 投票:0回答:1

我的数据框:

data = {'Col1': ['Bad Homburg', 'Bischofferode', 'Essen', 'Grabfeld OT Rentwertshausen','Großkrotzenburg','Jesewitz/Weg','Kirchen (Sieg)','Laudenbach a. M.','Nachrodt-Wiblingwerde','Rehburg-Loccum','Dingen','Burg (Dithmarschen)'],
        'Col2': ['Rehburg-Loccum','Grabfeld','Laudenbach','Kirchen','Jesewitz','Großkrotzenburg','Nachrodt-','Essen/Stadt','Bischofferode','Bad Homburg','Münster','Burg']}

df = pd.DataFrame(data)

我的 df 中有两列,如下所示:

col1 col2
巴特洪堡 雷堡-洛库姆
比肖费罗德 格拉布菲尔德
埃森 劳登巴赫
格拉布菲尔德 OT 伦特沃茨豪森 基兴
大克罗岑堡 耶塞维茨
耶塞维茨/韦格 大克罗岑堡
基兴(西格) 纳赫罗特-
劳登巴赫M. 埃森/施塔特
纳赫罗特-维布林韦尔德 比肖费罗德
雷堡-洛库姆 巴特洪堡
丁根 明斯特
伯格(迪特马尔申) 伯格

我想在 Col2 中查找 col1 数据。如果该项目存在,我想在 Lookup_Value 列下的同一行中写入,并且我还评论匹配的百分比。以下是我的预期结果:

col1 col2 查找值 评论
巴特洪堡 雷堡-洛库姆 巴特洪堡 100% 匹配
比肖费罗德 格拉布菲尔德 比肖费罗德 100% 匹配
埃森 劳登巴赫M. 埃森/施塔特 最佳匹配
格拉布菲尔德 OT 伦特沃茨豪森 基兴 格拉布菲尔德 最佳匹配
大克罗岑堡 耶塞维茨 大克罗岑堡 100% 匹配
耶塞维茨/韦格 大克罗岑堡 耶塞维茨 最佳匹配
基兴(西格) 纳赫罗特- 基兴 最佳匹配
劳登巴赫 埃森/施塔特 劳登巴赫M. 最佳匹配
纳赫罗特-维布林韦尔德 比肖费罗德 纳赫罗特- 最佳匹配
雷堡-洛库姆 巴特洪堡 雷堡-洛库姆 100% 匹配
丁根 明斯特 没有匹配
伯格(迪特马尔申) 伯格 伯格 最佳匹配

我正在尝试这种方法,但不起作用:

def lookup_value_and_comment(row):
    col1_value = row['Col1']
    col2_value = row['Col2']
    
    if col1_value in col2_value:
        if col1_value == col2_value:
            return pd.Series([col1_value, '100% Matched'], index=['Lookup_value', 'Comment'])
        else:
            return pd.Series([col2_value, 'Best Possible Match'], index=['Lookup_value', 'Comment'])
    else:
        return pd.Series(['', 'No Match'], index=['Lookup_value', 'Comment'])

df[['Lookup_value', 'Comment']] = df.apply(lookup_value_and_comment, axis=1)

print(df)
python dataframe vlookup string-matching textmatching
1个回答
0
投票

这里

col1_value == col2_value
您检查当前行
col1_value
与当前
row col2_value
,但您需要检查列col2的所有行。 由于行并不完全相同,我通过分隔符对列 col1、col2 进行分词:
-, /, and space
到创建列
col3, col4
的列表中。

import pandas as pd

df['col3'] = df['col1'].str.split('-|/| ')
df['col4'] = df['col2'].str.split('-|/| ')

def lookup_value_and_comment(row):
    col1_value = row['col1']
    col3_value = row['col3'][0]
    ind = df['col4'].str[0].isin([col3_value])

    if (df['col2'].isin([col1_value])).any():
        return pd.Series([col1_value, '100% Matched'],
                         index=['Lookup_value', 'Comment'])
    elif ind.any():
        return pd.Series([df.loc[ind, 'col2'].values[0],
                          'Best Possible Match'], index=['Lookup_value', 'Comment'])
    else:
        return pd.Series(['', 'No Match'], index=['Lookup_value', 'Comment'])



df[['Lookup_value', 'Comment']] = df.apply(lookup_value_and_comment, axis=1)

print(df)

输出:

                           col1              col2                             col3                  col4      Lookup_value              Comment
0                   Bad Homburg    Rehburg-Loccum                   [Bad, Homburg]     [Rehburg, Loccum]       Bad Homburg         100% Matched
1                 Bischofferode          Grabfeld                  [Bischofferode]            [Grabfeld]     Bischofferode         100% Matched
2                         Essen  Laudenbach a. M.                          [Essen]  [Laudenbach, a., M.]       Essen/Stadt  Best Possible Match
3   Grabfeld OT Rentwertshausen           Kirchen  [Grabfeld, OT, Rentwertshausen]             [Kirchen]          Grabfeld  Best Possible Match
4               Großkrotzenburg          Jesewitz                [Großkrotzenburg]            [Jesewitz]   Großkrotzenburg         100% Matched
5                  Jesewitz/Weg   Großkrotzenburg                  [Jesewitz, Weg]     [Großkrotzenburg]          Jesewitz  Best Possible Match
6                Kirchen (Sieg)         Nachrodt-                [Kirchen, (Sieg)]          [Nachrodt, ]           Kirchen  Best Possible Match
7                    Laudenbach       Essen/Stadt                     [Laudenbach]        [Essen, Stadt]  Laudenbach a. M.  Best Possible Match
8         Nachrodt-Wiblingwerde     Bischofferode         [Nachrodt, Wiblingwerde]       [Bischofferode]         Nachrodt-  Best Possible Match
9                Rehburg-Loccum       Bad Homburg                [Rehburg, Loccum]        [Bad, Homburg]    Rehburg-Loccum         100% Matched
10                       Dingen           Münster                         [Dingen]             [Münster]                               No Match
11          Burg (Dithmarschen)              Burg           [Burg, (Dithmarschen)]                [Burg]              Burg  Best Possible Match
© www.soinside.com 2019 - 2024. All rights reserved.