I have a dataframe df
as follows:
Col1 Val1 Val2 Val3
a v1 v1 z1
b v1 v3 x1
c v2 v2 x2
I want get the following:
Col1 Val1 Val2 Val3
b v1 v3 x1
c v2 v2 x2
I tried the following:
df = df[df['Val1'] != 'v1']
df = df[df['Val2'] != 'v1']
But the above step gets me only third row of df
. I only want to get rid of rows when both columns are not equal to v1
CodePudding user response:
Check any
out = df[df[['Val1','Val2']].ne('v1').any(1)]
Out[338]:
Col1 Val1 Val2 Val3
1 b v1 v3 x1
2 c v2 v2 x2
CodePudding user response:
IIUC, you want to check that there is at least one column that is not v1 (using eq
and any
):
df[df[['Val1', 'Val2']].ne('v1').any(1)]
or, using another syntax: not both columns are v1 (with using eq
, all
and the boolean NOT ~
):
df[~df[['Val1', 'Val2']].eq('v1').all(1)]
output:
Col1 Val1 Val2 Val3
1 b v1 v3 x1
2 c v2 v2 x2
CodePudding user response:
Chain both masks by |
for bitwise OR
:
df = df[(df['Val1'] != 'v1') | (df['Val2'] != 'v1')]
print (df)
Col1 Val1 Val2 Val3
1 b v1 v3 x1
2 c v2 v2 x2