How to check if any value of a column is in a range (in between two values) in Pandas?
Asked Answered
C

6

31

I have a DataFrame, and I would like to check if any of the values (v) of a column satisfies x<=v<=y.

equal = any(df['columnX'] == value) # No problems here
in_between = any(x <= df['columnX'] <= y) # ValueError :/

The error I get is ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). But I am using any() already!

So what's the problem here? Why does it work with == but not with x<=v<=y?

Coughlin answered 20/10, 2016 at 13:53 Comment(0)
L
54

Use between to do this, it also supports whether the range values are included or not via inclusive arg:

In [130]:
s = pd.Series(np.random.randn(5))
s

Out[130]:
0   -0.160365
1    1.496937
2   -1.781216
3    0.088023
4    1.325742
dtype: float64

In [131]:
s.between(0,1)

Out[131]:
0    False
1    False
2    False
3     True
4    False
dtype: bool

You then call any on the above:

In [132]:
s.between(0,1).any()

Out[132]:
True
Luwian answered 20/10, 2016 at 13:56 Comment(2)
Would this be correct then? in_between = any(df['columnX'].between(x, y,inclusive=True))Coughlin
I'd do in_between = df['columnX'].between(x, y,inclusive=True).any() personally but yes that would workLuwian
P
16

You can just have two conditions:

df[(x <= df['columnX']) & (df['columnX'] <= y)]

This line will select all rows in df where the condition is satisfied.

Plotinus answered 20/10, 2016 at 13:58 Comment(0)
J
6

You can use custom function for running on the dataframe:

df1 = pd.DataFrame({'a':[1,1,2,2], 'b':[1,1,2,2], 'c':[2,-1,4,5]})
myfun = lambda row: row['b']<=row['a']<=row['c']
df1['Result'] = df1.apply(myfun, axis=1)
display(df1)
Joost answered 6/4, 2021 at 13:33 Comment(0)
J
3

If you like to see other column values, you could try

df.loc[ df.loc[:, 'columnX'].between(a, b), : ]
Jackstay answered 28/10, 2019 at 23:43 Comment(0)
N
1

Another way is to use query:

In [2]: df = pd.DataFrame({'num': range(0, 5), 'alpha': list('ABCDE')})

In [3]: df
Out[3]: 
   num alpha
0    0     A
1    1     B
2    2     C
3    3     D
4    4     E

In [4]: df.query('1 <= num <= 3')
Out[4]: 
   num alpha
1    1     B
2    2     C
3    3     D
Nightcap answered 11/7, 2022 at 20:47 Comment(0)
C
0

You also can compare using numpy stlye of compare by rows:

df1 = pd.DataFrame({'a':[1,1,2,2], 'b':[1,1,2,2], 'c':[2,-1,4,5]})
#    a  b  c
# 0  1  1  2
# 1  1  1 -1
# 2  2  2  4
# 3  2  2  5

upper_limit = np.atleast_2d([2,0,2,3]).T
# [[2]
#  [0]
#  [2]
#  [3]]

lower_limit = np.atleast_2d([-2,-1,0,-3]).T
# [[-2]
#  [-1]
#  [ 0]
#  [-3]]

(lower_limit <= df1) & (df1 <= upper_limit)
#        a      b      c
# 0   True   True   True
# 1  False  False   True
# 2   True   True  False
# 3   True   True  False
Condolence answered 12/9, 2023 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.