Yes, comparing each record in two large DataFrames, especially with a nested loop approach, can be quite slow, particularly if one or both DataFrames have hundreds of thousands of entries (as in your case of 400,000 records). The nested loop approach essentially has a time complexity of O(n*m), where n is the size of df and m is the size of df_plp. This means you could potentially perform billions of comparisons, which can indeed take a significant amount of time.
-
Use Vectorized Operations: Fuzzy matching libraries sometimes offer more efficient ways to handle comparisons directly across entire Series or DataFrames. If you're using fuzzywuzzy, check if they have batch processing or any other methods to speed up the process, like rapidfuzz, which may be faster and have similar APIs.
-
Use pandas Combine Functions: You might consider using pandas.apply to create pairs of rows that you want to compare. However, keep in mind that this still might be slow with large datasets.
import pandas as pd
from fuzzywuzzy import fuzz
# Example using pairwise comparison (not for 400k, but shows concept)
df['key'] = 1 # Create a dummy key for merging
df_plp['key'] = 1
merged = df.merge(df_plp, on='key', suffixes=('_left', '_right')).drop('key', axis=1)
merged['partial_ratio'] = merged.apply(lambda x: fuzz.partial_ratio(x['cust_name'], x['CUST_NM']), axis=1)
# Now merged has the comparison results
-
Chunking: You can break down the data into smaller chunks, particularly if you can ensure that you're not losing relevant comparisons. For instance, compare a chunk of df against df_plp.
-
Parallel Processing: If you have a multi-core processor, you can take advantage of parallel processing:
- Use the
concurrent.futures library to leverage ThreadPoolExecutor or ProcessPoolExecutor for parallelizing your comparison.
- Alternatively, libraries like Dask or Modin can help you work with larger-than-memory datasets and distribute computation.
Here's a basic example using concurrent.futures:
from concurrent.futures import ProcessPoolExecutor
import pandas as pd
from fuzzywuzzy import fuzz
def compute_partial_ratios(args):
name1, names2 = args
return [(name1, name2, fuzz.partial_ratio(name1, name2)) for name2 in names2]
with ProcessPoolExecutor() as executor:
results = list(
executor.map(compute_partial_ratios, [(name, df_plp['CUST_NM'].tolist()) for name in df['cust_name']])
)
# Flatten the results list if needed
flat_results = [item for sublist in results for item in sublist]
results_df = pd.DataFrame(flat_results, columns=['cust_name', 'CUST_NM', 'partial_ratio'])
-
Approximate Nearest Neighbor Search: If performance is critical and you are flexible with matching accuracy, consider using Approximate Nearest Neighbors (ANN) algorithms. Libraries like annoy or faiss can help to quickly find close matches without computing the full pairwise similarity.
-
Consider Filtering Before Comparison: If possible, implement some filtering to reduce the dataset before the comparison. For instance, if both columns contain some identifiable prefixes or limited patterns, you can filter down based on those characteristics first.
While comparing a large volume of records can be feasible, it's crucial to apply efficient algorithms or techniques to ensure that you can obtain the results in a reasonable timeframe. Leveraging parallel processing, optimized libraries, or chunking your data are worthwhile strategies to explore.