Paco Bahena
Paco Bahena

Reputation: 361

How to subset a pandas series based on value?

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

Upvotes: 13

Views: 30618

Answers (2)

Alexander
Alexander

Reputation: 109526

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

Upvotes: 16

DYZ
DYZ

Reputation: 57033

Assuming that by "greater or under x" you mean "not equal to x", you can use boolean indexing:

s[s!=x]    

Upvotes: 2

Related Questions