pandas 检查熊猫系列是否包含负值的快速方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51720780/
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 05:53:51  来源:igfitidea点击:

Quick way to check if the pandas series contains a negative value

pythonpandasseriesnegative-numberbooleanquery

提问by Krzysztof S?owiński

What is the quickest way to check if the given pandas series contains a negative value.

检查给定的Pandas系列是否包含负值的最快方法是什么。

For example, for the series sbelow the answer is True.

例如,对于s下面的系列,答案是True

s = pd.Series([1,5,3,-1,7])

0    1
1    5
2    3
3   -1
4    7
dtype: int64

回答by Sunitha

Use any

any

>>> s = pd.Series([1,5,3,-1,7])
>>> any(s<0)
True

回答by Joe

You can use Series.lt:

您可以使用Series.lt

s = pd.Series([1,5,3,-1,7])
s.lt(0).any()

Output:

输出:

True

回答by Mastisa

Use any function:

使用任何函数:

>>>s = pd.Series([1,5,3,-1,7])
>>>any(x < 0 for x in s)
True
>>>s = pd.Series([1,5,3,0,7])
>>>any(x < 0 for x in s)
False