How to use Series.any() function in pandas

The Series.any() function in pandas

The Series.any() function in pandas checks whether any element in a given Series is True, across the specified axis.

Syntax

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

Parameters

  • axis: This represents the axis in which the operation is to be performed.

  • bool_only: This includes only boolean values.

  • skipna: This parameter is used to skip NaN values. The default is True.

  • level: The default is None. It represents the level to be broadcasted (in the case of multilevel).

Code example

The following code will demonstrate how to use the Series.any() function in pandas.

import pandas as pd
import numpy as np
# create Series
num_series = pd.Series([np.nan, 1, 0, 4,np.nan, 2])
# using Series.any()
print(num_series.any())

Code explanation

  • Lines 1–2: We import the libraries pandas and numpy.

  • Line 4: We create the num_series series.

  • Lines 7: We check for any true value in the Series using the any() function.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources