Zanam
Zanam

Reputation: 4807

Pandas column comparison so that two columns simultaneously are not equal to a value

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

Upvotes: 0

Views: 515

Answers (3)

mozway
mozway

Reputation: 260640

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

Upvotes: 1

jezrael
jezrael

Reputation: 862671

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

Upvotes: 1

BENY
BENY

Reputation: 323236

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

Upvotes: 2

Related Questions