based on this link I was trying to do a fuzzy lookup : Apply fuzzy matching across a dataframe column and save results in a new column between 2 dfs:
import pandas as pd
df1 = pd.DataFrame(data={'Brand_var':['Johnny Walker','Guiness','Smirnoff','Vat 69','Tanqueray']})
df2 = pd.DataFrame(data={'Product':['J.Walker Blue Label 12 CC','J.Morgan Blue Walker','Giness blue 150 CC','tqry qiuyur qtre','v69 g nesscom ui123']})
I have 2 dfs df1 and df2 which needs to be mapped via a fuzzy lookup/any other method which suits.
Below is the code I am using:
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
compare = pd.MultiIndex.from_product([df1['Brand_var'],
df2['Product']]).to_series()
def metrics(tup):
return pd.Series([fuzz.ratio(*tup),
fuzz.token_sort_ratio(*tup)],
['ratio', 'token'])
compare.apply(metrics)
df = compare.apply(metrics).unstack().idxmax().unstack(0)
print(df)
Below is my output:
ratio token
----------------------------------------------------------
Giness blue 150 CC Guiness Guiness
J.Morgan Blue Walker Johnny Walker Johnny Walker
J.Walker Blue Label 12 CC Johnny Walker Johnny Walker
tqry qiuyur qtre Tanqueray Tanqueray
v69 g nesscom ui123 Guiness Guiness
Expected output:
ratio token
----------------------------------------------------------
Giness blue 150 CC Guiness Guiness
J.Morgan Blue Walker None None
J.Walker Blue Label 12 CC Johnny Walker Johnny Walker
tqry qiuyur qtre Tanqueray Tanqueray
v69 g nesscom ui123 Vat 69 Vat 69
Any suggestions what could be a better approach(not using fuzzy wuzzy is also fine) to get my desired output?
Thank you in advance. :)