close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to check if any value of a column is in a range (in between two values) in Pandas in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

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

  1. How to check if any value of a column is in a range (in between two values) in Pandas?

    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.

  2. check if any value of a column is in a range (in between two values) in Pandas

    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.

Method 1

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

Method 2

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.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read