ruedi
ruedi

Reputation: 5545

Pandas Mask on multiple Conditions

In my dataframe I want to substitute every value below 1 and higher than 5 with nan.

This code works

persDf = persDf.mask(persDf < 1000)

and I get every value as an nan but this one does not:

persDf = persDf.mask((persDf < 1) and (persDf > 5))

and I have no idea why this is so. I have checked the man page and different solutions on apparentely similar problems but could not find a solution. Does anyone have have an idea that could help me on this?

Upvotes: 13

Views: 41684

Answers (1)

Erfan
Erfan

Reputation: 42906

Use the | operator, because a value cant be < 1 AND > 5:

persDf = persDf.mask((persDf < 1) | (persDf > 5))

Another method would be to use np.where and call that inside pd.DataFrame:

pd.DataFrame(data=np.where((df < 1) | (df > 5), np.NaN, df), 
             columns=df.columns)

Upvotes: 15

Related Questions