pandas 如何根据值对熊猫系列进行子集化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42967848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:16:08  来源:igfitidea点击:

How to subset a pandas series based on value?

pandasseries

提问by Paco Bahena

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

我有一个Pandas系列对象,我想根据一个值对它进行子集化

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

我如何才能对它进行子集化,以便获得一个仅包含大于或小于 x 值的元素的系列对象。?

回答by Alexander

I believe you are referring to boolean indexingon a series.

我相信你指的是一个系列的布尔索引

Greater than x:

大于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):

小于x(即在 x 下):

s[s < x]  # or s[s.lt(x)]

回答by DYZ

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

假设“大于或小于x”的意思是“不等于x”,则可以使用布尔索引:

s[s!=x]