# How to count the number of NaN values in Pandas?

## How to count the number of NaN values in Pandas?

We might need to count the number of NaN values for each feature in the dataset so that we can decide how to deal with it. For example, if the number of missing values is quite low, then we may choose to drop those observations; or there might be a column where a lot of entries are missing, so we can decide whether to include that variable at all.

Method 1: Using describe()

We can use the describe() method which returns a table containing details about the dataset. The count property directly gives the count of non-NaN values in each column. So, we can get the count of NaN values, if we know the total number of observations.

 `import` `pandas as pd ` `import` `numpy as np` `   ` `# dictionary of lists ` `dict` `=` `{ ``'A'``:[``1``, ``4``, ``6``, ``9``], ` `        ``'B'``:[np.NaN, ``5``, ``8``, np.NaN], ` `        ``'C'``:[``7``, ``3``, np.NaN, ``2``],` `        ``'D'``:[``1``, np.NaN, np.NaN, np.NaN] } ` ` ` `# creating dataframe from the` `# dictionary ` `data ``=` `pd.DataFrame(``dict``) ` `   ` `data.describe()`

Output :

Method 2: Using sum()
The isnull() function returns a dataset containing True and False values. Since, True is treated as a 1 and False as 0, calling the sum() method on the isnull() series returns the count of True values which actually corresponds to the number of NaN values.

Counting NaN in a column :

We can simply find the null values in the desired column, then get the sum.

 `import` `pandas as pd` `import` `numpy as np` `   ` `# dictionary of lists ` `dict` `=` `{ ``'A'``:[``1``, ``4``, ``6``, ``9``], ` `        ``'B'``:[np.NaN, ``5``, ``8``, np.NaN], ` `        ``'C'``:[``7``, ``3``, np.NaN, ``2``],` `        ``'D'``:[``1``, np.NaN, np.NaN, np.NaN] } ` `   ` `# creating dataframe from the` `# dictionary ` `data ``=` `pd.DataFrame(``dict``) ` ` ` `# total NaN values in column 'B'` `print``(data[``'B'``].isnull().``sum``())`

Output :

`2`

Counting NaN in a row :

The row can be selected using loc or iloc. Then we find the sum as before.

 `import` `pandas as pd ` `import` `numpy as np` `   ` `# dictionary of lists ` `dict` `=` `{ ``'A'``:[``1``, ``4``, ``6``, ``9``],` `        ``'B'``:[np.NaN, ``5``, ``8``, np.NaN], ` `        ``'C'``:[``7``, ``3``, np.NaN, ``2``],` `        ``'D'``:[``1``, np.NaN, np.NaN, np.NaN] }   ` `   ` `# creating dataframe from the ` `# dictionary ` `data ``=` `pd.DataFrame(``dict``) ` ` ` `# total NaN values in row index 1` `print``(data.loc[``1``, :].isnull().``sum``())`

Output :

`1`

Counting NaN in the entire DataFrame :
To count NaN in the entire dataset, we just need to call the sum() function twice – once for getting the count in each column and again for finding the total sum of all the columns.

 `import` `pandas as pd ` `import` `numpy as np` `   ` `# dictionary of lists ` `dict` `=` `{``'A'``:[``1``, ``4``, ``6``, ``9``],` `        ``'B'``:[np.NaN, ``5``, ``8``, np.NaN],` `        ``'C'``:[``7``, ``3``, np.NaN, ``2``],` `        ``'D'``:[``1``, np.NaN, np.NaN, np.NaN]} ` `   ` `# creating dataframe from the` `# dictionary ` `data ``=` `pd.DataFrame(``dict``) ` ` ` `# total count of NaN values` `print``(data.isnull().``sum``().``sum``())`

Output :

`6`

Last Updated on October 19, 2021 by admin

## Pandas Index.valuesPandas Index.values

Python | Pandas Index.values Pandas Index is an immutable ndarray implementing an ordered, sliceable set.

## Pandas dataframe.drop_duplicates()Pandas dataframe.drop_duplicates()

Python – Pandas dataframe.drop_duplicates()   Python is a great language for doing data analysis, primarily

## Create pandas dataframe from lists using dictionaryCreate pandas dataframe from lists using dictionary

Create pandas dataframe from lists using dictionary Creating pandas data-frame from lists using dictionary can

## Pandas.cut() method in PythonPandas.cut() method in Python

Pandas.cut() method in Python Pandas cut() function is used to separate the array elements into

## Difference between loc() and iloc() in Pandas DataFrameDifference between loc() and iloc() in Pandas DataFrame

Difference between loc() and iloc() in Pandas DataFrame   Pandas library of python is very

## Python – Pandas Series.to_frame()Python – Pandas Series.to_frame()

Pandas Series.to_frame()   Pandas series is a One-dimensional ndarray with axis labels. The labels need

## Working with excel files using PandasWorking with excel files using Pandas

Working with excel files using Pandas Excel sheet is one of the most ubiquitous forms

## Creating a Pandas DataFrameCreating a Pandas DataFrame

How to Create a Pandas DataFrame   In the real world, a Pandas DataFrame will