How To Check If Any Value Is NaN In A Pandas DataFrame

To check if any value is NaN in a Pandas DataFrame, you can use the isna() method, which returns a boolean DataFrame of the same shape as the original DataFrame, with True for NaN values and False for non-NaN values. Then, you can use the any() function to check if there is any True value in the boolean DataFrame.

Here’s an example to demonstrate this:

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [1, 2, np.nan],
                   'B': [4, np.nan, 6],
                   'C': [7, 8, 9]})

# Check if any value is NaN in the DataFrame
if df.isna().any().any():
    print("There is at least one NaN value in the DataFrame")
    print("There is no NaN value in the DataFrame")


There is at least one NaN value in the DataFrame

In this example, the DataFrame df has one NaN value, so the output is "There is at least one NaN value in the DataFrame". If all values were non-NaN, the output would have been "There is no NaN value in the DataFrame".

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!