Check if All Elements of DataFrame are True
To check if all elements of a DataFrame are True in Pandas, along an axis: index or columns, call all() method on this DataFrame, with required values for parameters.
In this tutorial, we will learn the syntax of DataFrame.all() method and how to use this method to check if all the elements of given DataFrame are True over an axis, with examples.
Syntax
The syntax of pandas DataFrame.all() method is
DataFrame.all(axis=0, bool_only=None, skipna=True, level=None, **kwargs)
where
Parameter | Value | Description |
---|---|---|
axis | {0 or ‘index’, 1 or ‘columns’, None}. default is 0. | 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. |
bool_only | bool. default is None. | Include only boolean columns. If None, will attempt to use everything, then use only boolean data. Not implemented for Series. |
skipna | bool. default is True. | 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. |
level | int or level name. default is None. | If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. |
**kwargs | any. default is None. | Additional keywords have no effect but might be accepted for compatibility with NumPy. |
Return Value
This method returns
- Series : If level is not specified.
- DataFrame: If level is specified.
Examples
Check if Column-wise Values are All True
By default, axis=’index’ for all() method. So, call all() method on DataFrame with default values for parameters to check if column-wise values are all True.
Example.py
import pandas as pd
df = pd.DataFrame(
{'col_1': [True, True, True], 'col_2': [True, True, False]})
result = df.all()
print(result)
Output
col_1 True
col_2 False
dtype: bool
Check if Row-wise Values are All True
Pass axis=’columns’ to check if DataFrame has elements all True row-wise.
Example.py
import pandas as pd
df = pd.DataFrame(
{'col_1': [True, True, True], 'col_2': [True, True, False]})
result = df.all(axis='columns')
print(result)
Output
0 True
1 True
2 False
dtype: bool
Conclusion
In this Pandas Tutorial, we learned how to check if all the elements of given DataFrame are True along an axis, using pandas DataFrame.all() method.