2

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?

1 Answer 1

3

This is really simple:

df.query('cat1 == dog1 or cat1 == dog2')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.