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?


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, privacy policy and cookie policy

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