How to subset a pandas series based on value?

有些话、适合烂在心里 提交于 2019-12-12 10:43:26

问题


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


回答1:


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



回答2:


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

s[s!=x]    


来源:https://stackoverflow.com/questions/42967848/how-to-subset-a-pandas-series-based-on-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!