Eric
Eric

Reputation: 693

Pandas: How do I check for value match between columns in same dataframe?

I am a complete coding novice and have been experimenting with Pandas. This is my first post. Thank you in advance for your help!

I would like to remove any rows where cat1 does not match either dog1 or dog2. It does not have to match both, just one or the other.

   cat1   dog1   dog2
0   red   red    blue  
1   red   green  blue  
2   blue  red    blue  
3   blue  blue   green  
4   red   green  blue

I would like the end result to be as follows:

   cat1   dog1   dog2  
0   red   red    blue  
2   blue  red    blue  
3   blue  blue   green  

How do I do this?

Upvotes: 2

Views: 1279

Answers (1)

chrisaycock
chrisaycock

Reputation: 37930

This is really simple:

df.query('cat1 == dog1 or cat1 == dog2')

Upvotes: 3

Related Questions