ChaddersCheese
ChaddersCheese

Reputation: 51

Pandas df.loc multiple conditions not working

Can anybody tell me why this is not working and how to get the behaviour I want?

df = some dataframe
df = df.where((df['a'] != 1) & (df['b'] != 2))
or
df = df.loc[(df['a'] != 1) & (df['b'] != 2)]

I want to filter my df to only show rows where columns a and b are not simultaneously 1 and 2 respectively.

Currently it's removing any instances of df['a'] == 1 and df['b'] == 2.

Can anyone help me get the behaviour I want?

Upvotes: 0

Views: 1383

Answers (2)

mozway
mozway

Reputation: 260890

If you want the rows where not both conditions are False, you need to have at least one condition True. Use OR (|), not AND (&):

df = df.loc[(df['a'] != 1) | (df['b'] != 2)]

Upvotes: 0

Naveed
Naveed

Reputation: 11650

check for a and b to be respectively 1 and 2, then negate in filtering result


df = df.loc[~( (df['a'] == 1) & (df['b'] == 2) )]

Upvotes: 1

Related Questions