I have a Pandas dataframe that collects the names of vendors at which a transaction was made. As this data is automatically collected from bank statements, lots of the vendors are similar... but not quite the same. In summary, I want to replace the different permutations of the vendors' names with a single name.
I think I can work out a way to do it (see below), but I'm a beginner and this seems to me like it's a complex problem. I'd be really interested to see how more experienced coders would approach it.
I have a dataframe like this (in real life, it's about 20 columns and a maximum of around 50 rows):
Groceries Car Luxuries
0 Sainsburys Texaco wst453 Amazon
1 Sainsburys bur Texaco east Firebox Ltd
2 Sainsbury's east Shell wstl Sony
3 Tesco Shell p/stn Sony ent nrk
4 Tescos ref 657 Texac Amazon EU
5 Tesco 45783 Moto Amazon marketplace
I'd like to find the similar entries and replace them with the first instance of those entries, so I'd end up with this:
Groceries Car Luxuries
0 Sainsburys Texaco wst453 Amazon
1 Sainsburys Texaco wst453 Firebox Ltd
2 Sainsburys Shell wstl Sony
3 Tesco Shell wstl Sony
4 Tesco Texaco wst453 Amazon
5 Tesco Moto Amazon
My solution might be far from optimum. I was thinking of sorting alphabetically, then going through bitwise and using something like SequenceMatcher from difflib to compare each pair of vendors. If the similarity is above a certain percentage (I'm expecting to play with this value until I'm happy) then the two vendors will be assumed to be the same. I'm concerned that I might be using a sledgehammer to crack a nut, or it might take a long time (I'm not obsessed with performance, but equally I don't want to wait hours for the result).
Really interested to hear people's thoughts on this problem!