Search code examples
pythonpandasrows

Compare two columns and return the row numbers in Python


I have two columns A = [11, 24, 7, 7, 0, 4, 9, 20, 3, 5] and B = [5, 8, 9, 1, 11]. But they have different row numbers. I want to find if A has the same element as B does and return the row numbers of A and B. For example,

A and B have the same value 5,9, 11 and the returned matrix is C = [5, 9, 11]

The returned row number of A should be row_A = [9, 6, 0]

The returned row number of B should be row_B = [0, 2, 4]

Are there any function in python I can use? Thanks


Solution

  • This is numpy.intersect1d. Using the return_indices argument you can also get the indices in addition to the values. This will work if A/B are numpy arrays, pandas Series, or lists.

    Note, if your objects are pandas objects, the returned indices are the array indices. So if your Series don't have a RangeIndex starting from 0, you can slice their Index objects by the array locations to get the actual Series Index labels.

    import numpy as np
    
    vals, idxA, idxB = np.intersect1d(A, B, return_indices=True)
    
    vals
    #array([ 5,  9, 11])
    
    idxA
    #array([9, 6, 0])
    
    idxB
    #array([0, 2, 4])