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

In Python Pandas, what's the best way to check whether a DataFrame has one (or more) NaN values?

I know about the function pd.isnan, but this returns a DataFrame of booleans for each element. This post right here doesn't exactly answer my question either.

jwilner's response is spot on. I was exploring to see if there's a faster option, since in my experience, summing flat arrays is (strangely) faster than counting. This code seems faster:


For example:

    In [2]: df = pd.DataFrame(np.random.randn(1000,1000))

    In [3]: df[df > 0.9] =

    In [4]: %timeit df.isnull().any().any()
    100 loops, best of 3: 14.7 ms per loop

    In [5]: %timeit df.isnull().values.sum()
    100 loops, best of 3: 2.15 ms per loop

    In [6]: %timeit df.isnull().sum().sum()
    100 loops, best of 3: 18 ms per loop

    In [7]: %timeit df.isnull().values.any()
    1000 loops, best of 3: 948 µs per loop

df.isnull().sum().sum() is a bit slower, but of course, has additional information -- the number of NaNs.


Back to homepage or read more recommendations: