How to use fuzzy matching for deduplication
Duplicate records in the data can occur due to human mistakes such as misspellings, typos, wrong translations, and using abbreviations. Therefore, Data Scientists might need to deduplicate records generated or extracted by human inputs to analyze the data more accurately.
For example, consider a platform where students insert their names and the institute or school names they studied in. The task is to cluster the students in the same institution. But, looking at the data, we see a lot of misspellings, typos, and incomplete names. In this case, before analysis, we need to deduplicate the records.
For example, the three records below are identical, but because one has a typo and the other is lowercase, the system cannot recognize them as matching.
For this exercise, we need the following libraries:
[Embedded content: 638dc8394d5724afa135cdda319f9a88]
Reading the Data
The data contains three columns. Let’s consider target is institute_name:
[Embedded content: f592cd3d61562eabbd2d914222cea884]
Cleaning the Data
The recommended first step is to clean the text as much as possible by removing punctuation, special characters, extra spaces, and other unnecessary elements. Depending on your data, you may also apply additional cleaning techniques, such as removing stopwords.
[Embedded content: cb36bc7590f04fe33eeb63083178e879]
Stemming improves the integrity of institution names by standardizing variations, such as “institutions” and “institute.”
[Embedded content: 09b51ba91abd490b3e962ffa67f56c92]
Now, we obtain the cleaned institute_name_cleaned simply by applying the above functions.
[Embedded content: 2a246d7591d6fdbc2b42fadeed164c2d]
Compute Fuzzy Scores
One of the popular Python libraries to apply fuzzy matching is FuzzyWuzzy. It provides several distance computation functions:
fuzz.ratio(): Returns the Levenshtein distance ratio for two strings.fuzz.partial_ratio(): Returns the ratio for partial matches.fuzz.token_sort_ratio(): Returns the ratio for alphabetically sorted tokenized strings.
Depending on the data, any of the above distances might be applicable. For this exercise, I believe fuzz.token_sort_ratio() results more reliable.
Our target column is institute_name_cleaned, and we choose the fuzzy threshold to be 78.
[Embedded content: ae849a159743f26e9984cb84d7e4657d]
To save memory and the cost of the fuzzy process, we take the unique names and compute the fuzzy distance for them.
[Embedded content: 62fbac6fdb259a2997d4d7a3fac9c904]
Next, I want to extract the first two matches for each name and calculate their scores.
[Embedded content: 607a8306bab2e5037b931641f943ca9f]
Then, I create a temporary dataframe containing the names, matches, and their scores.
[Embedded content: acfce686cb632465dc89cceaedffcbe5]
For each match, we check if the score exceeds the threshold. If it does, we consider it a good match; otherwise, we do not. This approach is applied to both matches.
[Embedded content: 39bc35c3eaba6dabbd4404b4a5c594ca]
first we compute it for the first matches
At each step, we check whether we have already considered a match by adding it to the tuple_matches. First, we check the first matches,
[Embedded content: a4b79803effe2d92a01de50d6f3d801f]
and then we do the same thing for the second matches.
[Embedded content: 5262de5576fffd4ae69cbdbff5cfeedc]
Finally, we have completed the temporary dataframe, and it is ready to be used for finding duplicate records.
Duplicate Detection
In the section above, I created the fuzzy_matches column with the actual names that we are trying to match. If the fuzzy match score is higher or equal to the threshold, we replace the name with its fuzzy match.
The replace_word() function replaces a name or part of it in a text.
[Embedded content: bb5ef3f3110aa2c5cb62ffca10b54656]
Here, we replace the high score matches with the current values of the the fuzzy_match column.
[Embedded content: b9d1fee0f0a4570b28b613f2f17fa9f7]
If we find a duplicate for an institute name based on a fuzzy match, we keep it and store it in duplicated_fuzzy. This variable is a replacement for fuzzy_matching.
[Embedded content: 48abcf8e17303cb511933bb410dc4ac1]
This is how we detect the duplicate:
Summary
The summary of the steps is the below function.
[Embedded content: dfee8d2e9d45850f849e8007c03dd98b]
In Plain English 🚀
Thank you for being a part of the In Plain English community! Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: X | LinkedIn | YouTube | Discord | Newsletter
- Visit our other platforms: Stackademic | CoFeed | Venture | Cubed
- Tired of blogging platforms that force you to deal with algorithmic content? Try Differ
- More content at PlainEnglish.io


