Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subset a pandas series based on value?

Tags:

pandas

series

I have a pandas series object, and i want to subset it based on a value

for example:

s = pd.Series([1,2,3,4,5,6,7,8,9,10])

how can i subset it so i can get a series object containing only elements greater or under x value. ?

like image 832
Paco Bahena Avatar asked Mar 23 '17 05:03

Paco Bahena


People also ask

How do I apply a condition in Pandas series?

Applying an IF condition in Pandas DataFrame You then want to apply the following IF conditions: If the number is equal or lower than 4, then assign the value of 'True' Otherwise, if the number is greater than 4, then assign the value of 'False'


1 Answers

I believe you are referring to boolean indexing on a series.

Greater than x:

x = 5
>>> s[s > x]  # Alternatively, s[s.gt(x)].
5     6
6     7
7     8
8     9
9    10
dtype: int64

Less than x (i.e. under x):

s[s < x]  # or s[s.lt(x)]
like image 184
Alexander Avatar answered Sep 23 '22 07:09

Alexander