What does the any() method do in the pandas series?

PandasServer Side ProgrammingProgramming

The any() is one of the pandas.Series method, which is used to verify if there is any non-zero value present in the given series object.

The pandas.Series method “any()” will return a boolean value as an output. It will return True if any value in the given series is non-zero. otherwise, it will return False for all zero values of the given series object.

Example 1

import pandas as pd

# create a series
s = pd.Series([False, False])

print(s)

print("Output: ")
print(s.any())

Explanation

Let’s see an example, here we have created a pandas series object with all zero-values (nothing but False). And applied the any() method to the series object “s”.

Output

0 False
1 False
dtype: bool

Output:
False

In the above block, we can see a series with Boolean values, all are False, which is nothing but zero-values. We have seen the boolean value “False” as an output for any() method, which is due to there being no single non-zero value present in the given series object.

Example 2

import pandas as pd

# create a series
s = pd.Series([False, True])

print(s)

print("Output: ")
print(s.any())

Explanation

Let’s take another example by creating it with a non-zero value in it, here we have created a pandas series object with a zero value (False) and non-zero value (True). And then applied the any() method to the series object “s”.

Output

0 False
1 True
dtype: bool

Output:
True

The output of the any() method for this following example is “True” (can be seen in the above output block) which due to there is a non-zero value present in the given series object.

raja
Updated on 09-Mar-2022 06:05:52

Advertisements