Pandas any()
method is applicable to both series and Dataframe. It checks to see if any -or the value in the caller object (Dataframe or series) is not 0, and returns True for that. If all values are 0, False is returned.
Syntax: DataFrame.any (axis = 0, bool_only = None, skipna = True, level = None, ** kwargs)
Parameters:
axis: 0 or ’index’ to apply method by rows and 1 or ’columns’ to apply by columns.
bool_only: Checks for bool only series in Data frame, if none found, it will use only boolean values. This parameter is not for series since there is only one column.
skipna: Boolean value, If False, returns True for whole NaN column / row
level: int or str, specifies level in case of multilevelReturn type: Boolean series
Example # 1: index implementation
This example creates a sample data frame by passing a dictionary to the Pandas DataFrame ()
method. Null values are also passed to some indices using Numpy np.nan
to test null-valued behavior. Because this example implements the method for the index, the axis parameter remains at 0 (which means rows).
|
Output:
As shown in the output, since the last column all values are zero, hence False was returned only for this column.
Example # 2: column-wise
This example creates a sample data frame by passing a dictionary to the Pandas DataFrame ()
method as in the example above. But instead of passing 0 to the axis parameter, 1 is passed to implement for each value in each column.
|
Output:
As shown in the output, False was returned only for stro to where all values were 0 or NaN and 0.