Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

any

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?

like image 749
alec_djinn Avatar asked Oct 20 '16 13:10

alec_djinn


People also ask

How do you check if a value is between two numbers in Pandas?

Pandas between() method is used on series to check which values lie between first and second argument. inclusive: A Boolean value which is True by default. If False, it excludes the two passed arguments while checking.

How do you check if one value is present in another column in Pandas?

Use in operator on a Series to check if a column contains/exists a string value in a pandas DataFrame. df['Courses'] returns a Series object with all values from column Courses , pandas. Series. unique will return unique values of the Series object.

How do you use between conditions in Pandas?

Boolean Series in PandasThe between() function is used to get boolean Series equivalent to left <= series <= right. This function returns a boolean vector containing True wherever the corresponding Series element is between the boundary values left and right. NA values are treated as False.


1 Answers

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
like image 51
EdChum Avatar answered Oct 14 '22 22:10

EdChum