Pandas
DataFrame
NaN
Data Analysis
Python

How to check if any value is NaN in a 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

Checking whether a pandas DataFrame contains any missing values is a basic data-cleaning step, but it is easy to choose an expression that is more verbose than necessary. The key APIs are isna, any, and values.any, and the best form depends on whether you want one boolean for the whole frame, one boolean per column, or the exact locations of missing data.

The Simplest Whole-DataFrame Check

If the question is only "does this DataFrame contain at least one NaN or missing value," the most direct expression is:

python
1import pandas as pd
2import numpy as np
3
4
5df = pd.DataFrame(
6    {
7        "name": ["Ada", "Ben", None],
8        "score": [95, np.nan, 88],
9        "city": ["Toronto", "Austin", "Boston"],
10    }
11)
12
13has_missing = df.isna().values.any()
14print(has_missing)

df.isna() returns a boolean DataFrame of the same shape. Converting to .values gives the underlying NumPy array, and .any() collapses that array to a single boolean.

This is usually the clearest answer when you need one yes-or-no result.

Column-Wise And Row-Wise Checks

Sometimes you need more detail than a single boolean. In that case, keep the result at the pandas level and apply any along an axis.

python
1import pandas as pd
2import numpy as np
3
4
5df = pd.DataFrame(
6    {
7        "a": [1, 2, np.nan],
8        "b": [4, 5, 6],
9        "c": [None, 8, 9],
10    }
11)
12
13print(df.isna().any())
14print()
15print(df.isna().any(axis=1))

df.isna().any() returns one boolean per column. axis=1 returns one boolean per row.

This is useful when you want to find columns that need imputation or rows that should be excluded before training a model.

You can also filter rows directly.

python
rows_with_missing = df[df.isna().any(axis=1)]
print(rows_with_missing)

That pattern is common during exploratory data analysis.

isna Versus isnull

In pandas, isna() and isnull() are equivalent aliases.

python
print(df.isna().equals(df.isnull()))

Use whichever name your team finds clearer. Many developers prefer isna() because it matches dropna() and fillna().

The important part is understanding what pandas treats as missing. NaN, None, and NaT are all considered missing in the right contexts. Plain empty strings are not automatically treated as missing.

python
1import pandas as pd
2
3
4df = pd.DataFrame({"text": ["", None, "hello"]})
5print(df.isna())

The empty string is False in that output. If your pipeline treats empty strings as missing, convert them explicitly before the check.

Performance And Readability

For most code, df.isna().values.any() is good enough. If you want to stay fully in pandas, df.isna().to_numpy().any() is also a clear option.

python
has_missing = df.isna().to_numpy().any()
print(has_missing)

to_numpy() is explicit and often preferred over .values in modern pandas code because it makes the conversion intent clearer.

If the DataFrame is extremely large and you only need to know whether missing values exist in certain columns, checking those columns directly can avoid unnecessary work.

python
important_columns = ["name", "score"]
has_missing = df[important_columns].isna().to_numpy().any()
print(has_missing)

That is not a different algorithm, but it keeps the check aligned with the business rule.

Common Pitfalls

A common mistake is using Python's built-in any(df.isna()). That iterates over column labels instead of collapsing the boolean table the way you expect.

Another mistake is checking only for numpy.nan and forgetting that pandas also treats None and NaT as missing values in many cases.

Developers also often expect empty strings to count as missing automatically. They do not. If your dataset uses empty strings as placeholders, normalize them first.

Finally, be clear about the level of detail you need. df.isna().values.any() gives one boolean for the whole frame. df.isna().any() gives one boolean per column. Mixing those up leads to confusing downstream code.

Summary

  • Use df.isna().values.any() or df.isna().to_numpy().any() for one boolean across the whole DataFrame.
  • Use df.isna().any() for a per-column result.
  • Use df.isna().any(axis=1) for a per-row result.
  • 'isna() and isnull() are equivalent in pandas.'
  • Empty strings are not automatically missing values.
  • Normalize missing-value conventions before building data-cleaning rules.

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.