1

I need to filter my DataFrame based on two conditions. I need to filter out any observation where cause = 'fire' AND the flag = 1. It can be fire and flag = 0. It can be flag = 1 and cause != 'fire'. Just not both at the same time.

I tried the following:

 df.loc[(df['flag'] != 1) & (df['cause'] != 'Fire')]

But this gets rid of all 1s and/or fire. I need both conditions to be met to be filtered out. Not sure where I'm going wrong.

1 Answer 1

1

built a DF with contents you defined. Make sure you code your logic. You state NOT(cause=="Fire" AND flag==1)

import random
c = ["Fire","Water"]
df = pd.DataFrame({"flag":[random.randint(0,1) for r in range(10)], "cause":[c[random.randint(0,1)] for r in range(10)]})

df.loc[~((df['flag'] == 1) & (df['cause'] == 'Fire'))]
Sign up to request clarification or add additional context in comments.

2 Comments

But that filters out Fire with '0' for the flag.
updated - just code the logic you state and it works

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.