Pandas
NaN
Dataframe
Python
Data Analysis

How to find which columns contain any NaN value in Pandas dataframe

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Finding columns that contain any NaN values is a common data-quality check in pandas pipelines. The task is simple, but teams often use verbose loops that are slower and harder to maintain than vectorized operations. The best solution should identify affected columns, optionally count missing values, and integrate cleanly into validation reports. This article shows concise patterns for discovering NaN-containing columns in large DataFrames.

Core Sections

1. Get column mask with isna().any()

python
1import pandas as pd
2
3mask = df.isna().any()
4print(mask)

This returns a boolean Series indexed by column names.

2. Extract only columns that contain NaNs

python
nan_cols = df.columns[df.isna().any()].tolist()
print(nan_cols)

This is the most common one-liner for column-level missingness detection.

3. Include NaN counts per column

python
nan_counts = df.isna().sum()
nan_counts = nan_counts[nan_counts > 0].sort_values(ascending=False)
print(nan_counts)

Counts help prioritize cleanup efforts.

4. Filter DataFrame to problematic columns

python
bad_df = df.loc[:, df.isna().any()]

Useful for inspection notebooks and quality dashboards.

5. Distinguish NaN vs empty strings

Empty strings are not NaN by default. Normalize first if needed:

python
df = df.replace(r"^\s*$", pd.NA, regex=True)

Then rerun isna() checks.

6. Add validation gates

In ETL jobs, fail fast when critical columns contain NaNs:

python
required = ["id", "created_at"]
if df[required].isna().any().any():
    raise ValueError("Required columns contain missing values")

This prevents silent downstream data corruption.

Validation and production readiness

A working snippet is only the first step. To make the solution dependable, validate behavior under representative inputs and operating conditions. Build a small test matrix that includes normal cases, boundary values, and malformed data so failure modes are explicit. If the topic involves time, concurrency, or networking, add at least one test that simulates delayed execution and one test that verifies timeout handling. This catches race conditions and environment-specific bugs that rarely appear in local happy-path runs.

Operational clarity matters as much as correctness. Document assumptions near the implementation: runtime version, required dependencies, expected timezone or locale rules, and platform limitations. Ambiguous assumptions are a major source of production incidents because teammates run the same logic under different defaults. Use structured logs around critical branches and external calls so debugging does not require ad hoc reproduction. Logs should include identifiers and concise context, but avoid sensitive payloads.

For recurring jobs or frequently executed code paths, add observability and guardrails. Define simple success metrics, retry boundaries, and explicit rollback or fallback behavior. Silent retries with no upper limit can hide systemic failures and increase downstream impact. Keep a lightweight pre-deploy checklist in source control so changes remain auditable and repeatable across environments.

text
1release_checklist:
2  - tests cover edge cases and failure paths
3  - runtime and dependency versions documented
4  - logs/metrics confirm expected execution path
5  - retries and timeouts are bounded
6  - rollback or fallback plan is defined

Teams that treat these checks as part of the default implementation workflow usually spend less time on incident triage and more time shipping stable improvements.

Common Pitfalls

  • Iterating columns manually instead of using vectorized isna().any().
  • Treating empty strings as missing without normalization.
  • Reporting only a boolean flag without counts for triage.
  • Ignoring dtype coercion that introduces unexpected NaNs.
  • Failing to enforce required-column checks in production pipelines.

Summary

Use df.isna().any() to quickly detect which columns contain missing values and df.isna().sum() for severity. Normalize empty strings when relevant, and enforce required-field gates early. These vectorized checks are concise, performant, and easy to integrate into quality monitoring workflows.

In collaborative teams, documenting this exact workflow and enforcing it with simple CI or runbook checks prevents repeated mistakes and keeps behavior consistent across development, staging, and production environments.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.