0

In the pandas I use df[df['Column']!='value'] to get desired column values. But how can I use it for multiple condition?

I used this

import numpy as np
df[df['Column']!=np.nan and df['Column']!='value_x' and df['Column']!='value_y']

But it is throwing

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Rakshith N
  • 15
  • 6

1 Answers1

3

For compare missing values need Series.notna and because priority operators add () in next conditions, for bitwise AND use &:

df[df['Column'].notna() & (df['Column']!='value_x') & (df['Column']!='value_y')]

Another out of box solution is replace missing values, test if value_x or value_y and invert condition by ~:

df[~df['Column'].fillna('value_x').isin(['value_x','value_y'])]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252