Subsetting dataframe by same values in two columns

Question:

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

Asked By: Ruser_092

||

Answers:

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

Answered By: Utsav
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.