Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a pandas Series has at least one item greater than a value

Tags:

The following code will print True because the Series contains at least one element that is greater than 1. However, it seems a bit un-Pythonic. Is there a more Pythonic way to return True if a Series contains a number that is > a particular value?

import pandas as pd  s = pd.Series([0.5, 2]) print True in (s > 1) 

True

EDIT: Not only is the above answer un-Pythonic, it will sometimes return an incorrect result for some reason. For example:

s = pd.Series([0.5]) print True in (s < 1) 

False

like image 979
ChaimG Avatar asked Dec 08 '15 05:12

ChaimG


People also ask

How do I compare values in Pandas series?

Compare two Series objects of the same length and return a Series where each element is True if the element in each Series is equal, False otherwise. Compare two DataFrame objects of the same shape and return a DataFrame where each element is True if the respective element in each DataFrame is equal, False otherwise.

How do you check if a column contains a particular value in Pandas?

You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas. series. isin() , str.

How do you do greater than in Pandas?

Pandas DataFrame: ge() function The ge() function returns greater than or equal to of dataframe and other, element-wise. Equivalent to ==, =!, <=, <, >=, > with support to choose axis (rows or columns) and level for comparison.

Can ILOC be used in series?

iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.


1 Answers

You could use any method to check if that condition is True at least for the one value:

In [36]: (s > 1).any() Out[36]: True 
like image 157
Anton Protopopov Avatar answered Sep 29 '22 14:09

Anton Protopopov