w3resource

Pandas Series: any() function

Test whether any element is true over requested Pandas axis

The any() function is used to check whether any element is True, potentially over an axis.

Returns False unless there at least one element within a series or along a Dataframe axis that is True or equivalent (e.g. non-zero or non-empty).

Syntax:

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

Parameters:

Name Description Type/Default Value Required / Optional
axis Indicate which axis or axes should be reduced.
  • 0 / ‘index’ : reduce the index, return a Series whose index is the original column labels.
  • 1 / ‘columns’ : reduce the columns, return a Series whose index is the original index.
  • None : reduce all axes, return a scalar.
{0 or ‘index’, 1 or ‘columns’, None}
Default Value: 0
Required
bool_only Include only boolean columns. If None, will attempt to use everything, then use only boolean data. Not implemented for Series. bool
Default Value: None
Required
skipna Exclude NA/null values. If the entire row/column is NA and skipna is True, then the result will be True, as for an empty row/column. If skipna is False, then NA are treated as True, because these are not equal to zero. bool
Default Value: True
Required
level If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a scalar. nt or level name
Default Value: None
Required
kwargs Additional keywords have no effect but might be accepted for compatibility with NumPy. any
Default Value: None
Required

Returns: scalar or Series
If level is specified, then, Series is returned; otherwise, scalar is returned.

Example - Series:

For Series input, the output is a scalar indicating whether any element is True.

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([False, False]).any()

Output:

False

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([True, False]).any()

Output:

True

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([]).any()

Output:

False

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([np.nan]).any()

Output:

False

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.Series([np.nan]).any(skipna=False)

Output:

True

Example - DataFrame:

Whether each column contains at least one True element (the default).

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"P": [2, 3], "Q": [0, 4], "R": [0, 0]})
df

Output:

   P  Q  R
0  2  0  0
1  3  4  0
Pandas Series any image

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"P": [2, 3], "Q": [0, 4], "R": [0, 0]})
df.any()

Output:

P     True
Q     True
R    False
dtype: bool

Example - Aggregating over the columns:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"P": [True, False], "Q": [2, 3]})
df

Output:

     P	Q
0	True	2
1	False	3

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"P": [True, False], "Q": [2, 3]})
df.any(axis='columns')

Output:

0    True
1    True
dtype: bool

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"P": [True, False], "Q": [2, 0]})
df

Output:

     P	Q
0	True	2
1	False	0

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"P": [True, False], "Q": [2, 0]})
df.any(axis='columns')

Output:

0     True
1    False
dtype: bool

Example - Aggregating over the entire DataFrame with axis=None:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({"P": [True, False], "Q": [2, 3]})
df.any(axis=None)

Output:

True

Example - any for an empty DataFrame is an empty Series:

Python-Pandas Code:

import numpy as np
import pandas as pd
pd.DataFrame([]).any()

Output:

Series([], dtype: bool)

Previous: Test whether all element is true over requested Pandas axis
Next: Compute the lag-N autocorrelation in Pandas



Follow us on Facebook and Twitter for latest update.