Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 595
Krzysztof Słowiński Avatar asked Aug 07 '18 07:08

Krzysztof Słowiński


People also ask

How do I check pandas series value?

isin() function check whether values are contained in Series. It returns a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.


2 Answers

Use any

>>> s = pd.Series([1,5,3,-1,7])
>>> any(s<0)
True
like image 146
Sunitha Avatar answered Sep 30 '22 06:09

Sunitha


You can use Series.lt :

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

Output:

True
like image 31
Joe Avatar answered Sep 30 '22 06:09

Joe