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. ?
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'
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)]
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