1

I am working with pandas df and I am trying to make all the numbers that are outside of range set as null, but having trouble

df['Numbers'] = df['Numbers'].mask((df['Numbers']< -10) & (df['Numbers']> 10), inplace=True)

So I want to keep the numbers between -10 and 10, if the numbers are outside of those two numbers, it should be set as null.

What am I doing wrong here?

1
  • This answer matches your approach most closely and mirrors richardec's answer below. Though you can also use Series between: df.loc[~df['Numbers'].between(-10, 10), 'Numbers'] = np.nan like this answer Commented Feb 1, 2022 at 0:07

2 Answers 2

0

One thing that immediately strikes out at me is that you're using & with your two conditions, so you're basically trying to select all numbers that are both less than -10 and greater than 10...which isn't gonna work ;)

I'd rewrite your code like this:

df.loc[df['Numbers'].lt(-10) | df['Numbers'].gt(10), 'Numbers'] = np.nan
Sign up to request clarification or add additional context in comments.

2 Comments

Why use .lt() instead of <?
@Barmar I personally prefer using .eq, .ne, .lt, .gt etc., instead of their corresponding Python operators, because you don't have to wrap the conditions using them in parentheses when you're joining them with & or |. I.e., you can say df[a.eq(b) | x.eq(y)] instead of df[(a == b) | (x == y)]. Now, in that little example, the latter does look more readable, but the former I prefer in larger settings where there are more complex and more complex conditions.
0

I would do it like this:

df['Numbers'] = df['Numbers'].where((df['Numbers']>-10) & (df['Numbers']<10))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.