Search code examples
pythonpandassubset

Subsetting dataframe by same values in two columns


I have a dataframe that looks like below:

code_1 code_2
a1 a1
a2 a1
b1 b2
b3 b3

What I want to do here is that I want to subset the dataframe by selecting the rows that have same values in 'code_1' and 'code_2'

The final output would look like below:

code_1 code_2
a1 a1
b3 b3

Thank you


Solution

  • Let's try query

    Code

    df.query('code_1 == code_2')
    

    Output

        code_1  code_2
    0   a1      a1
    3   b3      b3
    

    If we want the index to be sequential

    df.query('code_1 == code_2').reset_index(drop=True)
    

    Output

        code_1  code_2
    0   a1      a1
    1   b3      b3
    

    More details about query can be found here