Reputation: 7514
The following code will print True because the Series contains at least one element that is greater than 1. However, it seems a bit un-Pythonic. Is there a more Pythonic way to return True if a Series contains a number that is greater than a particular value?
import pandas as pd
s = pd.Series([0.5, 2])
print True in (s > 1) # True
Not only is the above answer un-Pythonic, it will sometimes return an incorrect result for some reason. For example:
s = pd.Series([0.5])
print True in (s < 1) # False
Upvotes: 36
Views: 79977
Reputation: 23111
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
Upvotes: 2
Reputation: 31672
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
Upvotes: 54