Pandas DataFrame.all: Check if All Elements are True in a DataFrame


Pandas DataFrame.all

The DataFrame.all method in pandas is used to check if all elements in a DataFrame or along a specified axis evaluate to True. It is particularly useful for logical operations on DataFrames.


Syntax

The syntax for DataFrame.all is:

DataFrame.all(axis=0, bool_only=False, skipna=True, **kwargs)

Here, DataFrame refers to the pandas DataFrame on which the operation is performed.


Parameters

ParameterDescription
axisSpecifies the axis along which the operation is performed. Use 0 or 'index' to check columns, and 1 or 'columns' to check rows. Defaults to 0.
bool_onlyIf True, only boolean columns are included in the operation. Defaults to False.
skipnaIf True, missing values (NaN) are ignored. Defaults to True.
**kwargsAdditional keyword arguments to pass to the method.

Returns

A Series or scalar value indicating whether all elements along the specified axis evaluate to True.


Examples

Checking if All Elements in a DataFrame Column are True

This example demonstrates how to use all to check if all elements in each column of a DataFrame are True.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [True, True, True], 'B': [True, False, True]})

# Check if all elements in each column are True
result = df.all()
print(result)

Output

A     True
B    False
dtype: bool

Checking if All Elements in a DataFrame Row are True

This example shows how to use all to check if all elements in each row of a DataFrame are True.

Python Program

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [True, True, False], 'B': [True, False, True]})

# Check if all elements in each row are True
result = df.all(axis=1)
print(result)

Output

0     True
1    False
2    False
dtype: bool

Checking if All Elements are True with Missing Values

This example demonstrates how all handles missing values (NaN) when checking if all elements are True.

Python Program

import pandas as pd

# Create a DataFrame with missing values
df = pd.DataFrame({'A': [True, None, True], 'B': [True, False, None]})

# Check if all elements in each column are True (ignoring missing values)
result = df.all(skipna=True)
print(result)

Output

A     True
B    False
dtype: bool

Checking if All Elements are True in Boolean Columns Only

This example shows how to use all with the bool_only parameter to check only boolean columns in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame with mixed data types
df = pd.DataFrame({'A': [True, True, True], 'B': [1, 0, 1], 'C': [True, False, True]})

# Check if all elements in boolean columns are True
result = df.all(bool_only=True)
print(result)

Output

A     True
C    False
dtype: bool

Summary

In this tutorial, we explored the DataFrame.all method in pandas. Key takeaways include:

  • Using all to check if all elements in a DataFrame or along a specified axis evaluate to True.
  • Handling missing values with the skipna parameter.
  • Restricting the operation to boolean columns with the bool_only parameter.
  • Understanding the flexibility of all for logical operations on DataFrames.

Python Libraries