import pandas as pd
df = pd.DataFrame({'A': [False, False], 'B': [True, False]})
print(df.any())
Expected Output:
A False
B True
dtype: bool
The DataFrame.any() method evaluates whether at least one element in a DataFrame or Series evaluates to True along a specified axis. It is frequently used for outlier detection, flag verification, and anomaly filtering.
DataFrame.any(axis=0, bool_only=None, skipna=True, **kwargs)axis: 0 or 'index' (column-wise reduction), 1 or 'columns' (row-wise reduction).bool_only: Restrict evaluation to boolean columns only.skipna: Exclude NaN values (default True).Setting axis=1 tests whether any column in a given row satisfies a specific condition (e.g., checking if an employee recorded over 40 hours of overtime in any quarter).
import pandas as pd
df_overtime = pd.DataFrame({
'Q1': [5, 0, 12],
'Q2': [0, 0, 15],
'Q3': [8, 0, 0]
}, index=['Emp1', 'Emp2', 'Emp3'])
# Check if employee worked overtime in ANY quarter
worked_overtime = (df_overtime > 0).any(axis=1)
print(worked_overtime)
Expected Output:
Emp1 True
Emp2 False
Emp3 True
dtype: bool
Chaining isna() with any() is a standard Pandas workflow for detecting missing values across columns or rows.
import pandas as pd
import numpy as np
df_data = pd.DataFrame({
'ID': [101, 102, 103],
'SCORE': [88.5, np.nan, 92.0]
})
# Identify which columns contain missing values
has_missing = df_data.isna().any()
print(has_missing)
Q: How can I check if ANY cell in an entire DataFrame is True?
Chain the method twice: df.any().any() evaluates down columns first and then collapses the resulting Series to a single boolean.
Q: Does any() treat non-zero numbers as True?
Yes, in Python boolean logic, 0 is False and any non-zero numeric value (positive or negative) evaluates to True.
0 for index/columns, 1 for rows) or strings ('index', 'columns').
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.