all() functionThe pandas all() function returns a single boolean value for each row or column. The function returns True if all the values in the given axis are true or False if any value is false.
all(axis=0, bool_only=None, skipna=True, level=None)
axis: This represents the index to perform the operation. The value 0 indicates rows while 1 indicates the columns.
bool_only: This is an optional parameter that specifies whether or not to check for only boolean columns. The default value is None.
skipna: This is an optional parameter. The default is True. If set to False, it wonβt skip null values. Instead, it will return True for NaN values.
level: This is an optional parameter. The default is None. It specifies the level (in the case of multilevel) to count along.
It returns true or false for each row/column.
The following code will demonstrate how to use the any() function in pandas:
import pandas as pdimport numpy as np# Create a DataFramedf = pd.DataFrame({'A': [1, np.nan, 0, 2, 0, np.nan, 4],'B': [1, 1, 3, 5, 0, 0, 5],'C': [np.nan, 0, np.nan, 0, 1, 0, 0]})# finds if value is true or not using all()print(df.all(axis=1))
In the code above:
Lines 1β2: We import the needed libraries.
Lines 5β7: We create a DataFrame, df, from a dictionary.
Line 13: We use the any() function to return a True value if any value in the column axis is true. Otherwise, False is returned.