W3docs

How to check if any value is NaN in a Pandas DataFrame

You can use the isna() method to check for NaN values in a Pandas DataFrame.

You can use the isna() method to check for NaN values in a Pandas DataFrame. The method returns a DataFrame of the same shape as the original, but with True or False values indicating whether each element is NaN or not. Here is an example code snippet:

Check if any value is NaN in a Pandas DataFrame in Python

python— editable, runs on the server
Info

Watch a video course: Python - The Practical Guide

You can also use isnull() method for the same purpose

Check if any value is NaN in a Pandas DataFrame in Python using the isnull method

nan_mask = df.isnull()

You can also use the following code snippet to check if any value is NaN in the DataFrame:

Check if any value is NaN in a Pandas DataFrame in Python using the isna and any methods

if df.isna().any().any():
    print("Dataframe contains NaN values")
else:
    print("Dataframe contains no NaN values")

or

Check if any value is NaN in a Pandas DataFrame in Python using the isnull and any methods

if df.isnull().any().any():
    print("Dataframe contains NaN values")
else:
    print("Dataframe contains no NaN values")