Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

python - Quicker way to perform fuzzy string match in pandas

Is there any way to speed up the fuzzy string match using fuzzywuzzy in pandas.


I have a dataframe as extra_names which has names that I want to run fuzzy matches for with another dataframe as names_df.

>> extra_names.head()

     not_matching
0 Vij Sales
1 Crom Electronics 
2 REL Digital
3 Bajaj Elec
4 Reliance Digi

>> len(extra_names)
6500

>> names_df.head()

         names   types
0 Vijay Sales        1
1 Croma Electronics  1
2 Reliance Digital   2
3 Bajaj Electronics  2
4 Pai Electricals    2

>> len(names_df)
250

As of now, I'm running the logic using the following code, but its taking forever to complete.

choices = names_df['names'].unique().tolist()

def fuzzy_match(row):
    best_match = process.extractOne(row, choices)
    return best_match[0], best_match[1] if best_match else '',''

%%timeit
extra_names['best_match'], extra_names['match%'] = extra_names['not_matching'].apply(fuzzy_match)

As I'm posting this question, the query is still running. Is there any way to speed up this fuzzy string matching process?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's try difflib:

import difflib
from functools import partial

f = partial(
    difflib.get_close_matches, possibilities=names_df['names'].tolist(), n=1)

matches = extra_names['not_matching'].map(f).str[0].fillna('')
scores = [
    difflib.SequenceMatcher(None, x, y).ratio() 
    for x, y in zip(matches, extra_names['not_matching'])
]

extra_names.assign(best=matches, score=scores)

       not_matching               best     score
0         Vij Sales        Vijay Sales  0.900000
1  Crom Electronics  Croma Electronics  0.969697
2       REL Digital   Reliance Digital  0.666667
3        Bajaj Elec  Bajaj Electronics  0.740741
4     Reliance Digi   Reliance Digital  0.896552

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...