The Python Oracle

Check if a pandas Series has at least one item greater than a value

This video explains
Check if a pandas Series has at least one item greater than a value

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow

--

Chapters
00:00 Question
00:51 Accepted answer (Score 45)
01:07 Thank you

--

Full question
https://stackoverflow.com/questions/3414...

Accepted answer links:
[any]: http://pandas.pydata.org/pandas-docs/ver...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 52


You could use any method to check if that condition is True at least for the one value:

In [36]: (s > 1).any()
Out[36]: True



ANSWER 2

Score 1


in operator a.k.a __contains__() method checks if a specific value exists as an index in a Series.

s = pd.Series([0.5], index=['a'])

'a' in (s > 1)          # True
'b' in s                # False

As a side note, in operator used on dataframes checks if a value exists as a column label.

df = pd.DataFrame([[1]], columns=['a'])
'a' in df               # True
'b' in df               # False

In other words, the fact that the in operator returns True or False has nothing to do with whether (s > 1) has any True values in it or not. In order to make the membership test work, the values must be accessed.

True in (s < 1).values  # True

Reducing the values into a single boolean value (as suggested by @Anton Protopopov) is the canonical way to this task. Python's built-in any() function may be called as well.

any(s > 1)              # False
s.gt(1).any()           # False

(s < 1).any()           # True
s.lt(1).any()           # True