I'm trying to locate the most recent rows within my Dataframe that contain the same values in two separate columns.

Presently, I am doing this slowly with looping, but I suspect there's a way to cleverly use the apply method or some other vectorized function to do this faster. My present code:

def enumerate_matching(df):
    a = list(df['A'])
    b = list(df['B'])
    matching = []

    for i in range(0, len(a)-1):
        for j in range(i+1, len(b)):
            if a[i] == b[j]:
    return matching

Is there a faster method to do this?


2 Answers 2


you could use set to get the intersection (it has a complexity logarithmic in the size of the sets a and b)

 a = set(df['A'])
 b = set(df['B'])

If you want to do the matching line by line, you should do:

np.sum(df['A'] == df['B'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.