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?
CodePudding user response:
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) )]
CodePudding user response:
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)]