If s
is a pandas.Series
, I know I can do this:
b = s < 4
or
b = s > 0
but I can't do
b = 0 < s < 4
or
b = (0 < s) and (s < 4)
What is the idiomatic pandas method for creating a boolean series based on the logical AND / OR / NOT of other boolean series?
Pandas Series: between() function The 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.
Pandas between() method is used on series to check which values lie between first and second argument.
found it... the &
operator works, but you need to use parentheses to get the precedence right and avoid an error:
>>> import pandas as pd
>>> s1 = pd.Series([0,1,2,3,4,5,6,0,1,2,3,4])
>>> (s1 < 4) & (s1 > 0)
0 False
1 True
2 True
3 True
4 False
5 False
6 False
7 False
8 True
9 True
10 True
11 False
dtype: bool
>>> s1 < 4 & s1 > 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\app\python\anaconda\1.6.0\lib\site-packages\pandas\core\generic.py",
line 698, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
You can also use .between
:
s1.between(0, 4, inclusive=False)
A bit verbose but as it doesn't have to create 2 intermediary series it should be faster (admittedly untested).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With