How to count the NaN values in a column in a Python Pandas DataFrame?


To count the NaN values in a column in a Pandas DataFrame, we can use the isna() method with sum.

Steps

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

  • Print the series, s.

  • Count the number of NaN present in the series.

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

  • Print the input DataFrame.

  • Find NaN count column wise.

  • Print the count DataFrame.

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:
", s count = s.isna().sum() print "NAN count in series: ", count 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 "
Input DataFrame is:
", df count = df.isna().sum() print "
NAN count in DataFrame:
", count

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 count in series: 4

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 count in DataFrame:
x  2
y  2
z  3
dtype: int64

Updated on: 30-Aug-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements