9

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

For example, for the series s below the answer is True.

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

0    1
1    5
2    3
3   -1
4    7
dtype: int64
Krzysztof Słowiński
  • 6,239
  • 8
  • 44
  • 62

3 Answers3

19

Use any

>>> s = pd.Series([1,5,3,-1,7])
>>> any(s<0)
True
Sunitha
  • 11,777
  • 2
  • 20
  • 23
5

You can use Series.lt :

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

Output:

True
Joe
  • 12,057
  • 5
  • 39
  • 55
0

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
mastisa
  • 1,875
  • 3
  • 21
  • 39