Pandas DataFrame all() Method

import pandas as pd 

df = pd.DataFrame({'A': [True, True], 'B': [True, False]})
print(df.all())

Expected Output:

A     True
B    False
dtype: bool

1. Overview and Syntax

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.

Method Signature: DataFrame.all(axis=0, bool_only=None, skipna=True, **kwargs)
Parameters:
• 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).
Returns: A Series of boolean values indicating whether every item along the target axis is True.

2. Column-Wise vs Row-Wise Evaluation

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

3. Numeric Non-Zero Checks & Data Validation

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)

4. Handling Missing Values (skipna)

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))

5. Frequently Asked Questions (FAQs)

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.

6. Common Errors & Troubleshooting

ValueError: The truth value of a Series is ambiguous
Cause: Using standard Python if df: statements on DataFrames.
Fix: Use df.all().all() or df.empty to evaluate full DataFrame truthiness.
Interactive Companion Jupyter Notebook

Explore interactive code cells, dynamically generate classes, test type vs isinstance comparisons, and run self-validating assertions.

View on GitHub Open In Colab

7. Related Tutorials




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer