Signup/Sign In
PUBLISHED ON: MARCH 31, 2021

Pandas Series any() Method

In this tutorial, we will learn the Python pandas Series.any() method. This method can be used to check whether the elements in the Series are True or False. This method returns the True at least one element in the Series is True otherwise it returns False.

Below is the syntax of the Series.any() method.

Syntax

Series.any(axis=0, bool_only=None, skipna=True, level=None, **kwargs)

Example: Pandas Series.any() Method

Let's create a Series and check the elements using the Series.any() method. In this example, we created three series with different elements, for the Series s_1 and s_2 the Series.any() method returns True because both the Series contains at least one element as 'True' and for the Series s_3, it returns False as it contains 'False' as all elements. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
s_1=pd.Series([True,True])
s_2=pd.Series([False,False])
s_3=pd.Series([True,False])
print(s_1.any())
print(s_2.any())
print(s_3.any())


True
False
True

Example 2: Pandas Series.any() Method

Here, in this example, we are checking the Series that consists of null values, '0' and '1' as elements and also with the empty Series. For the Series s_1 and s_3, the Series.any() method returns True fas these Series consist one element as True and '1' respectively and for empty Series and for number '0' it returns False. See the below example.

#importing pandas as pd
import pandas as pd
import numpy as np
#creating Series
s_1=pd.Series([True,np.NaN,np.NaN])
s_2=pd.Series([])
s_3=pd.Series([1])
s_4=pd.Series([0])
print(s_1.any(skipna=False))
print(s_2.any())
print(s_3.any())
print(s_4.any())


True
False
True
False

Example 3: Pandas Series.any() Method

Here, in this example, we will check the two Series. The Series.any() method returns True only if the given condition matches otherwise it returns False. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
s_1=pd.Series([1,2,7])
s_2=pd.Series([4,5,6])
s_3=pd.Series([7,8,9])
print(any(s_1>s_2))
print(any(s_2>s_3))


True
False

Conclusion

In this tutorial, we learned how to use the Series.any() method of the python pandas.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.