kittygirl
kittygirl

Reputation: 2443

How to exclude rows based on multi column value conditions in pandas dataframe?

There's pandas dataframe ad below:

email             score
[email protected]      A
[email protected]      A
[email protected]      C
[email protected]      B

I want to exclude rows with email [email protected] and [email protected].Expect result as below:

email            score
[email protected]      A
[email protected]      B

I tried 3 times but failed:

df=df[df.email !='[email protected]' & df.email !='[email protected]' ]
TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]


df=df[df.email !='[email protected]' && df.email !='[email protected]' ]
SyntaxError: invalid syntax


df=df[df.email !='[email protected]' | '[email protected]' ]
TypeError: unsupported operand type(s) for |: 'str' and 'str'

What's the problem?

Upvotes: 1

Views: 2262

Answers (1)

U13-Forward
U13-Forward

Reputation: 71610

You have to surround it by parenthesis:

df = df[(df.email != '[email protected]') & (df.email != '[email protected]')]

That said, it would be easier with isin:

df = df[~df.email.isin(['[email protected]', '[email protected]'])]

And now:

print(df)

Is gonna be the expected output.

Upvotes: 2

Related Questions