9

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?

ruedi
  • 5,365
  • 15
  • 52
  • 88
  • replace `and` with `|` – warped May 11 '19 at 19:38
  • 1
    A value cant be both? Its `or`, better yet use `|` and in this cause I would use: `np.where((persDf< 1) | (persDf> 5), np.NaN, persDf) ` – Erfan May 11 '19 at 19:38
  • @Erfan: I want to get a dataframe back so I stick to the mask command. Strangely enough using or gives me a Exception has occurred: ValueError but | works as I wish. Thanks. You can create an answer I gonna accept if you wish. – ruedi May 11 '19 at 19:53
  • Glad it worked :) – Erfan May 11 '19 at 19:57

1 Answers1

12

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)
Erfan
  • 40,971
  • 8
  • 66
  • 78