1
votes

I have dataframe:

df =
    original_title                     title
      Mexico Oil                    Gas Summit
      Mexico Oil                    Gas Summit

I have to fuzzy match the entities of these two(original_title & title) columns and get a score. below is my code:

compare = pd.MultiIndex.from_product([ df['original_title'],df ['title'] ]). to_series()
def metrics (tup):
    return pd.Series([fuzz.partial_ratio(*tup),fuzz.token_sort_ratio(*tup)], ['partial', 'token'])

compare.apply(metrics)

The above code compares each original title with the whole column of title. while, I want it to compare each original title with title in each row. My expected outcome would be:

df =
 original_title                    title                 partial_ratio
 Mexico Oil                      Africa Oil                   81
 French Property Exhibition      French                      100
 French Exhibition               French Exhibition           100

Would appreciate your help. Thanks

1

1 Answers

7
votes

You can use the Dataframes apply() function as follows:

df['partial_ratio'] = df.apply(lambda x: fuzz.partial_ratio(x['original_title'], x['title']), axis=1)

This gives the results I think you're after (although the numbers are slightly different):

...    partial_ratio
...    78
...    83
...    100
...    100
...    100