python数据模糊匹配,使用python中两个数据集的模糊匹配创建标志

我尝试了两个df的模糊比较,就我的研究而言,没有快速的方法来做。使用4fuzz方法也会降低脚本的速度。一种方法是使用'工艺提取酮()`并创建一个函数:from fuzzywuzzy import process

def fw_process(row_df1):

# Select the addresses from df2 with same postal_code

df2_select_add = df2['address'][df2['postal_code'] == row_df1['postal_code']]

ad_1 = row_df1['address']

# Find the best match for ad_1 in df2_select_add and get the ratio with [1]

# for the name of df2_select_add , use [0]

if process.extractOne(ad_1, df2_select_add)[1] >= 80:

return 'Y'

else:

return 'N'

然后要在df1中创建列标志,请执行以下操作:

^{pr2}$

注意:名称df2不是作为函数的参数调用的,这不是一种更干净的方式,但是如果在代码中使用这个名称定义它,它就可以工作了。在

如果您想保留4fuzz方法,那么可以按照相同的想法创建函数:from fuzzywuzzy import fuzz

def fw_fuzz ( row_df1):

# Select the addresses from df2 with same postal_code

df2_select_add = df2['address'][df2['postal_code'] == row_df1['postal_code']]

ad_1 = row_df1['address']

# Get the max of the max of the 4 fuzz comparison between ad_1 and df2_select_add

if max (df2_select_add.apply(lambda x: max(fuzz.ratio(ad_1, x), fuzz.partial_ratio(ad_1, x),

fuzz.token_sort_ratio(ad_1, x),fuzz.token_set_ratio(ad_1, x)))) >= 80:

return 'Y'

else:

return 'N'

然后:df1['flag'] = df1.apply(fw_fuzz, axis=1)