Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas series filtering between values

Tags:

python

pandas

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?

like image 339
Jason S Avatar asked Jan 13 '15 19:01

Jason S


People also ask

How do you get between values in pandas?

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.

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.


2 Answers

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().
like image 194
Jason S Avatar answered Sep 27 '22 20:09

Jason S


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).

like image 44
fantabolous Avatar answered Sep 27 '22 20:09

fantabolous