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

PandasServer Side ProgrammingProgramming

To check if any value is NaN in a Pandas DataFrame, we can use isnull().values.any() method.

Steps

  • Make a series, s, one-dimensional ndarray with axis labels (including time series).

  • Print the series, s.

  • Check whether NaN is present or not.

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.

  • Print the input DataFrame.

  • Check whether NaN is present or not.

Example

 Live Demo

import pandas as pd
import numpy as np

s = pd.Series([1, np.nan, 3, np.nan, 3, np.nan, 7, np.nan, 3])
print "Input series is:\n", s
present = s.isnull().values.any()
print "NAN is present in series: ", present

df = pd.DataFrame(
   {
      "x": [5, np.nan, 1, np.nan],
      "y": [np.nan, 1, np.nan, 10],
      "z": [np.nan, 1, np.nan, np.nan]
   }
)
print "\nInput DataFrame is:\n", df
present = df.isnull().values.any()
print "\nNAN present in DataFrame:", present

Output

Input series is:
0  1.0
1  NaN
2  3.0
3  NaN
4  3.0
5  NaN
6  7.0
7  NaN
8  3.0
dtype: float64
NAN is present in series: True

Input DataFrame is:
    x     y      z
0 5.0    NaN    NaN
1 NaN    1.0    1.0
2 1.0    NaN    NaN
3 NaN    10.0   NaN
NAN present in DataFrame: True
raja
Published on 30-Aug-2021 11:48:12
Advertisements