My solution with references below: Apply fuzzy matching across a dataframe column and save results in a new column

df.loc[:,'fruits_copy'] = df['fruits']

compare = pd.MultiIndex.from_product([df['fruits'],
                                      df['fruits_copy']]).to_series()

def metrics(tup):
    return pd.Series([fuzz.ratio(*tup),
                      fuzz.token_sort_ratio(*tup)],
                     ['ratio', 'token'])

compare.apply(metrics)

                       ratio  token
apple      apple         100    100
           apples         91     91
           orange         36     36
           apple tree     67     67
           oranges        33     33
           mango          20     20
apples     apple          91     91
           apples        100    100
           orange         33     33
           apple tree     62     62
           oranges        46     46
           mango          18     18
orange     apple          36     36
           apples         33     33
           orange        100    100
           apple tree     25     25
           oranges        92     92
           mango          55     55
apple tree apple          67     67
           apples         62     62
           orange         25     25
           apple tree    100    100
           oranges        24     24
           mango          13     13
oranges    apple          33     33
           apples         46     46
           orange         92     92
           apple tree     24     24
           oranges       100    100
           mango          50     50
mango      apple          20     20
           apples         18     18
           orange         55     55
           apple tree     13     13
           oranges        50     50
           mango         100    100
Answer from ah bon on Stack Overflow
🌐
Towards Data Science
towardsdatascience.com › fuzzywuzzy-find-similar-strings-within-one-column-in-a-pandas-data-frame-99f6c2a0c212
FuzzyWuzzy: Find Similar Strings within one column in Python | Towards Data Science
March 30, 2022 - FuzzyWuzzy has four scorer options to find the Levenshtein distance between two strings. In this example, I would check on the token sort ratio and the token set ratio since I believe they are more suitable for this dataset which might have mixed words order and duplicated words. I pick four brand names and find their similar names in the Brand column...
Discussions

python - Pandas: Matching string using fuzzywuzzy and retrieve the value of other column - Stack Overflow
I am having a dataframe df as follows. 33KV Feeder 11KV Feeder Circle_name Codes Shree Gopal SPS 33ShreeGopal_DPS Jai Balaji ... More on stackoverflow.com
🌐 stackoverflow.com
How do I compare strings in two data frames using fuzzywuzzy?
-> use process import fuzzywuzzy from fuzzywuzzy import process import pandas as pd data = {'NamesInCol1':['Joe','Sean','David','Elie','Mary'], 'NamesInCol2':['Jean', 'Joel', 'dave','Mama','Choney']} df = pd.DataFrame(data) list1 = df['NamesInCol1'].tolist() list2 = df['NamesInCol2'].tolist() for name in list1: closest = process.extractOne(name, list2) print(name, closest) Result: Joe ('Joel', 86) Sean ('Jean', 75) David ('dave', 67) Elie ('Joel', 50) Mary ('Mama', 50) if you want more than one result per name from list1 in list2: for name in list1: closest = process.extract(name, list2, limit=2) print(name, closest) More on reddit.com
🌐 r/learnpython
4
1
July 19, 2022
python - Match similar column elements using pandas and fuzzwuzzy - Stack Overflow
I have an excel file that contains 1000+ company names in one column and about 20,000 company names in another column. The goal is to match as many names as possible. The problem is that the nam... More on stackoverflow.com
🌐 stackoverflow.com
python - String Similarity using fuzzywuzzy on big data - Code Review Stack Exchange
I have a file in which I was to check the string similarity within the names in a particular column. I use fuzzywuzzy token sort ratio algorithm as it is required for my use case. here is the code,... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
May 3, 2018
🌐
DataCamp
datacamp.com › tutorial › fuzzy-string-python
Fuzzy String Matching in Python Tutorial | DataCamp
January 15, 2026 - It is also possible to calculate the Levenshtein similarity ratio based on the Levenshtein distance. This can be done using the following formula: where |a| and |b| are the lengths of a sequence and b sequence, respectively. One of the most popular packages for fuzzy string matching in Python was historically FuzzyWuzzy.
🌐
Medium
medium.com › analytics-vidhya › matching-messy-pandas-columns-with-fuzzywuzzy-4adda6c7994f
Matching Messy Pandas columns with FuzzyWuzzy | by Khalid El Mouloudi | Analytics Vidhya | Medium
February 1, 2024 - In this article, I’m going to show you how to use the Python package FuzzyWuzzy to match two Pandas dataframe columns based on string similarity; the intended outcome is to have each value of column A matched with the closest corresponding value in column B, which is then put in the same row.
🌐
Analytics Vidhya
analyticsvidhya.com › home › fuzzy string matching – a hands-on guide
Fuzzy String Matching – A Hands-on Guide
May 1, 2025 - In Python, libraries like FuzzyWuzzy and Levenshtein provide efficient ways to measure string similarity using methods such as Levenshtein distance, Partial Ratio, Token Sort Ratio, and Token Set Ratio.
🌐
HAR Data Extractor
jonathansoma.com › lede › algorithms-2017 › classes › fuzziness-matplotlib › fuzzing-matching-in-pandas-with-fuzzywuzzy
Fuzzing matching in pandas with fuzzywuzzy
The Python package fuzzywuzzy has a few functions that can help you, although they’re a little bit confusing! I’m going to take the examples from GitHub and annotate them a little, then we’ll use them. ... # fuzz is used to compare TWO strings from fuzzywuzzy import fuzz # process is ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-do-fuzzy-matching-on-pandas-dataframe-column-using-python
How to do Fuzzy Matching on Pandas Dataframe Column Using Python? - GeeksforGeeks
May 30, 2021 - fuzz.token_set_ratio: It tries to rule out differences in the strings, it returns the maximum ratio after calculating the ratio on three particular sub-string sets in python ... At first, we will create two dictionaries. Then we will convert it into pandas data frames and create two empty lists for storing the matches later than as shown below: ... from fuzzywuzzy import fuzz from fuzzywuzzy import process import pandas dict1 = {'name': ["aparna", "pankaj", "sudhir", "Geeku"]} dict2 = {'name': ["aparn", "arup", "Pankaj", "sudhir c", "Geek", "abc"]} # converting to pandas dataframes dframe1 = pd.DataFrame(dict1) dframe2 = pd.DataFrame(dict2) # empty lists for storing the # matches later mat1 = [] mat2 = [] # printing the pandas dataframes dframe1.show() dframe2.show()
🌐
Stack Overflow
stackoverflow.com › questions › 57145526 › pandas-matching-string-using-fuzzywuzzy-and-retrieve-the-value-of-other-column
python - Pandas: Matching string using fuzzywuzzy and retrieve the value of other column - Stack Overflow
import pandas as pd from fuzzywuzzy import fuzz,process df = pd.read_csv(file_path,encoding="ISO-8859-1") feeder_input = input() # Lets say Mithapur circle_input = input() # Lets say SPS match_feeder = process.extract(feeder_input,data['11KV Feeder'],scorer=fuzz.token_sort_ratio) match_circle = process.extract(feeder_input,data['Circle name'],scorer=fuzz.token_sort_ratio) if match_feeder[0][1] > 60 match_circle[0][1] > 90: z = df.loc[data[['11Kv Feeder','33Kv Feeder']].str.contains(match_feeder[0][0]).any(1) & df['Circle_name'].str.contains(match_circle[0][0]),['Codes']].iloc[0] print(z)
Find elsewhere
🌐
Towards Data Science
towardsdatascience.com › home › latest › string matching with fuzzywuzzy
String Matching With FuzzyWuzzy | Towards Data Science
January 28, 2025 - Instead of trying to format the strings in order to match, Fuzzywuzzy uses a some similarity ratio between two sequences and returns the similarity percentage. For a more detailed description, take a look over the documentation. Let’s start by importing the necessary libraries and go over a simple example. Although it isn’t required, python-Levenshtein is highly recommended with FuzzyWuzzy.
🌐
Reddit
reddit.com › r/learnpython › how do i compare strings in two data frames using fuzzywuzzy?
r/learnpython on Reddit: How do I compare strings in two data frames using fuzzywuzzy?
July 19, 2022 -

I have two data frames (df1 and df2). df1 contains a column with around 100 company names. I want to check to what extent they match with the company names in df2 (of which there are around 1,000). The columns in both data frames are entitled ‘Name’.

I would like the output to show df1[‘Name’] and the closest matching company name in df2 and the score.

I can use fuzzywuzzy to compare two individual company names and get a score. For example

fuzz.token_sort_ratio(‘Amazon plc’, ‘Amazon PLC’)

However, I’m struggling to work out how to compare a column of strings against another in a separate data frame.

🌐
Medium
dabidsheen.medium.com › fuzzywuzzy-string-similarity-matching-in-python-a4ac48d61e15
Fuzzywuzzy: String Similarity Matching in Python | by David Shin | Medium
October 21, 2020 - The Levenshtein distance between the words ‘Honda’ and ‘Hyundai’ is equal to 3. ‘Y’ is added to the string, ‘O’ is converted to a ‘U’, and a final ‘I’ is added to the end of the string. Regardless of which letters you choose to add or substitute, the total count is 3. Let’s get started! A simple install function to get the ball rolling. ... Now, let’s import some modules. from fuzzywuzzy import fuzz from fuzzywuzzy import process
🌐
Neudesic
neudesic.com › blog › fuzzywuzzy-using-python
FuzzyWuzzy Using Python - Neudesic
July 2, 2024 - There are four popular types of fuzzy matching logic supported by the FuzzyWuzzy Python library: Ratio – uses pure Levenshtein Distance based matching · Partial Ratio – matches based on best substrings · Token Sort Ratio – tokenizes the strings and sorts them alphabetically before matching · Token Set Ratio – tokenizes the strings and compares the intersection and remainder · The simple ratio method calculates the similarity ratio between two strings using the Levenshtein distance.
Top answer
1 of 2
4

To avoid processing each dish so many times, you can use this to process them only 1 time:

dishes = ["pizza with bacon", "pizza with extra cheese", "vegetarian pizza", "cheese and bacon pizza", "bacon with pizza"]

processedDishes = []

for dish in dishes:
    processedDishes.append(fuzz._process_and_sort(dish, True, True))

for dish1, dish2 in itertools.combinations(enumerate(processedDishes), 2):  
    if fuzz.ratio(dish1[1], dish2[1]) >= 85:
        print(dishes[dish1[0]], dishes[dish2[0]])

You can then combine this with @scnerd solution to add multiprocessing

If you know your data is all the same type, you can further optimize it:

dishes = ["pizza with bacon", "pizza with extra cheese", "vegetarian pizza", "cheese and bacon pizza", "bacon with pizza"]

processedDishes = []
matchers = []

for dish in dishes:
    if dish:
        processedDish = fuzz._process_and_sort(dish, True, True)
        processedDishes.append(processedDish)
        matchers.append(fuzz.SequenceMatcher(None, processedDish))


for dish1, dish2 in itertools.combinations(enumerate(processedDishes), 2):
    matcher = matchers[dish1[0]]
    matcher.set_seq2(dish2[1])
    ratio = int(round(100 * matcher.ratio()))

    if ratio >= 85:
        print(dishes[dish1[0]], dishes[dish2[0]])

Update: checked how these ratios are calculated, here is a more efficient answer that avoids a lot of checks between pairs:

dishes = ["pizza with bacon", "pizza with extra cheese", "vegetarian pizza", "cheese and bacon pizza", "bacon with pizza", "a"]


processedDishes = []
matchers = []

for dish in dishes:
    if dish:
        processedDish = fuzz._process_and_sort(dish, True, True)
        processedDishes.append({"processed": processedDish, "dish": dish})


processedDishes.sort(key= lambda x: len(x["processed"]))

for idx, dish in enumerate(processedDishes):
    length = len(dish["processed"])
    matcher = fuzz.SequenceMatcher(None, dish["processed"])
    for idx2 in range(idx + 1, len(processedDishes)):
        dish2 = processedDishes[idx2]
        if 2 * length / (length + len(dish2["processed"])) < 0.85: # upper bound
            break

        matcher.set_seq2(dish2["processed"])

        if matcher.quick_ratio() >= 0.85 and matcher.ratio() >= 0.85: # should also try without quick_ratio() check
            print(dish["dish"], dish2["dish"])
2 of 2
8

The first algorithmic recommendation is to use itertools.combinations instead of .permutations, since you don't care about order. This assumes fuzz.token_sort_ratio(str_1, str_2) == fuzz.token_sort_ratio(str_2, str_1). There are half as many combinations as there are permutations, so that gives you a free 2x speedup.

This code also lends itself easily to parallelization. On an i7 (8 virtual cores, 4 physical), you could probably expect this to give you ~4-8x speedup, but that depends on a lot of factors. The least intrusive way to do this is to use multiprocessing.Pool.map or .imap_unordered:

import multiprocessing as mp

def ratio(strings):
    s1, s2 = strings
    return s1, s2, fuzz.token_sort_ratio(s1, s2)

with open('D:\\Sim_Score.csv', 'w') as f1:
    writer = csv.writer(f1, delimiter='\t', lineterminator='\n', )
    writer.writerow(['tag4'])

    with mp.Pool() as pool:
        for s1, s2, r in pool.imap_unordered(ratio, itertools.combinations(Dishes, 2)):
            if r > threshold_ratio:
                writer.writerow([(s1, s2, r)])

All told, I'd expect these changes to give you 5-10x speedup, depending heavily on the number of cores you have available.

For reference, a generator comprehension version of this might look something like:

with mp.Pool() as pool:
    writer.writerows((s1, s2, r)
                     for s1, s2, r in pool.imap_unordered(ratio, itertools.combinations(Dishes, 2))
                     if r > threshold_ratio)

That version has a some, but very little, performance improvement over the non-comprehension version, and IMO is harder to read/maintain.

One other minor thing I noticed in testing my code was that fuzzywuzzy recommends installing python-Levenshtein in order to run faster; when I did so, it ran about 20x slower than when it used the built-in SequenceMatcher. When I uninstalled python-Levenshtein it got fast again. That seems very odd to me, but it's certainly something worth trying.

Finally, if performance is important, you could consider digging into what fuzz.token_sort_ratio does to see if you could remove some repeated work. For example, it's tokenizing and sorting each string again every time you pass it in, so maybe you could pre-tokenize/sort the strings and only run the ratio logic inside the main loop. A quick dig tells me that token_sort_ratio is two main steps:

  1. Preprocess each string using fuzz._process_and_sort

  2. Run fuzz.partial_ratio against the processed strings

I'm not able to get this to run faster at the moment, but I'll edit and update this answer if I can get that approach to work well.

🌐
Typesense
typesense.org › posts › fuzzy string matching in python (with examples)
Fuzzy string matching in Python (with examples) | Typesense
import Levenshtein as levenshtein ... = levenshtein.ratio(str1, str2) Fuzzywuzzy is a more feature-rich library for computing string similarity and performing fuzzy string matching in Python....
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to do fuzzy string matching in pandas dataframes
How to Do Fuzzy String Matching in Pandas Dataframes | Towards Data Science
January 20, 2025 - TheFuzz is an open-source Python package formally known as "FuzzyWuzzy." It uses the Levenshtein edit distance to calculate the similarity string similarity.
🌐
Stack Overflow
stackoverflow.com › questions › 58633634 › using-fuzzymatching-algorithm-to-determine-whether-there-are-similar-words-in-na
python - Using FuzzyMatching Algorithm to determine whether there are similar words in Name column - Stack Overflow
import pandas as pd import numpy as np #Fuzzywuzzy used for string matching from fuzzywuzzy import fuzz from fuzzywuzzy import process #Reading Data in and creating DB data = pd.read_csv("data(2).csv", encoding="ISO-8859-1") print(data.head) data = data.sort_values('Name') print(data.head) df = pd.read_csv("data(2).csv") org_list = data['Name'] threshold = 1300 def find_match(x): #fuzz.partial_token_sort_ratio attempts to account for similar strings out of order match = process.extract(x, org_list, limit=2, scorer=fuzz.partial_token_sort_ratio)[1] match = match if match[1]>threshold else np.nan return match df['match found'] = [find_match(row) for row in df['Name']] print(df) #Transposing CSV transposed_data = data.T transposed_df = df.T transposed_df.to_csv(r'Desktop\Transposed_Data(1).csv') transposed_data.to_csv(r'Desktop\Transposed_Data.csv')
🌐
Bryan Ross
rossbj92.github.io › Fuzzy-Matching
Fuzzy String Matching with Pandas and fuzzywuzzy - Bryan Ross
February 8, 2020 - Fuzzywuzzy utilizes the Levenshtein Distance to determine string similarity.
🌐
Built In
builtin.com › data-science › fuzzy-matching-python
Fuzzy String Matching in Python: Intro to Fuzzywuzzy | Built In
Fuzzy string matching is the process of finding strings that approximately match a pattern. It does this by calculating the number of operations needed to transform one string to the other.
🌐
Seatgeek
chairnerd.seatgeek.com › fuzzywuzzy-fuzzy-string-matching-in-python
FuzzyWuzzy: Fuzzy String Matching in Python - ChairNerd
July 8, 2011 - The library is called “Fuzzywuzzy”, the code is pure python, and it depends only on the (excellent) difflib python library. It is available on Github right now. The simplest way to compare two strings is with a measurement of edit distance. For example, the following two strings are quite ...