I use the SequenceMatcher ratio to match two dataframe with the best ratio.

I want to check first if the score A and AA is good then check if the score between B is BB is good then if the score between C and CC is good, then I add the line

I tried this function, but it doesn't work:

from difflib import SequenceMatcher
def similar(a, b):
    ratio = SequenceMatcher(None, a, b).ratio()
    return ratio
order = []
score = []
for index, row in df1.iterrows():
    maxima = [similar(row['A'], j) for j in df2['AA']]
    best_ratio = max(maxima)
    if best_ratio > 0.9:     
        maxima2 = [similar(row['B'], j) for j in df2['BB']]
        best_ratio2 = max(maxima2)
        if best_ratio2 > 0.9:
           maxima3 = [similar(row['C'], j) for j in 
           best_ratio = max(maxima3)
           best_row = np.argmax(maxima3)
df2 = df2.iloc[order].reset_index()
merge = pd.concat([df1, df2], axis=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.