Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas isin() function for continuous intervals

Tags:

python

pandas

Let's say I want to construct a dummy variable that is true if a number is between 1 and 10, I can do:

df['numdum'] = df['number'].isin(range(1,11))

Is there a way to do that for a continuous interval? So, create a dummy variable that is true if a number is in a range, allowing for non-integers.

like image 787
Elliott Avatar asked Jun 03 '15 15:06

Elliott


2 Answers

Series objects (including dataframe columns) have a between method:

>>> s = pd.Series(np.linspace(0, 20, 8))
>>> s
0     0.000000
1     2.857143
2     5.714286
3     8.571429
4    11.428571
5    14.285714
6    17.142857
7    20.000000
dtype: float64
>>> s.between(1, 14.5)
0    False
1     True
2     True
3     True
4     True
5     True
6    False
7    False
dtype: bool
like image 73
DSM Avatar answered Sep 20 '22 06:09

DSM


This works:

df['numdum'] = (df.number >= 1) & (df.number <= 10)
like image 45
Elliott Avatar answered Sep 19 '22 06:09

Elliott