import pandas as pd
df = pd.DataFrame({'A': [True, True], 'B': [True, False]})
print(df.all())
Expected Output:
A True
B False
dtype: bool
The DataFrame.all() method evaluates whether all elements in a DataFrame or Series evaluate to True along a requested axis. In Python, non-zero numbers evaluate to True while 0, empty values, or False evaluate to False.
DataFrame.all(axis=0, bool_only=None, skipna=True, **kwargs)axis: 0 or 'index' (reduce along columns), 1 or 'columns' (reduce along rows).bool_only: Include only boolean columns (True/False).skipna: Exclude NaN values during evaluation (default True).By default, axis=0 tests if all elements in each column are true. Setting axis=1 tests if all elements in each row are true.
import pandas as pd
df_scores = pd.DataFrame({
'PASS_MATH': [True, True, True],
'PASS_ENG': [True, False, True]
}, index=['Student1', 'Student2', 'Student3'])
# Row-wise evaluation: Check if students passed all subjects
passed_all_subjects = df_scores.all(axis=1)
print(passed_all_subjects)
Expected Output:
Student1 True
Student2 False
Student3 True
dtype: bool
In numeric DataFrames, 0 acts as False, while non-zero values (positive or negative) evaluate as True. This makes all() ideal for data quality assertions.
import pandas as pd
df_inventory = pd.DataFrame({
'STOCK_A': [15, 20, 5],
'STOCK_B': [10, 0, 8]
})
# Verify all items in stock (non-zero)
in_stock = (df_inventory > 0).all()
print(in_stock)
With skipna=True (default), NaN values are ignored during evaluation. When skipna=False, encountering NaN alongside True values produces True unless explicit boolean logic dictates otherwise.
import pandas as pd
import numpy as np
df_nan = pd.DataFrame({'A': [True, np.nan], 'B': [False, np.nan]})
print(df_nan.all(skipna=True))
Q: How does all() differ from any() in Pandas?
all() requires every single entry along an axis to be True to return True. any() returns True if at least one entry along the axis is True.
Q: Can all() check an entire DataFrame into a single boolean?
Yes, chaining df.all().all() reduces the DataFrame first across columns, then across the resulting Series into a single scalar boolean value.
if df: statements on DataFrames.df.all().all() or df.empty to evaluate full DataFrame truthiness.
Explore interactive code cells, dynamically generate classes, test type vs isinstance comparisons, and run self-validating assertions.
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.