Open In App

Count NaN or missing values in Pandas DataFrame

Last Updated : 23 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In this article, we will see how to Count NaN or missing values in Pandas DataFrame using isnull() and sum() method of the DataFrame.

1. DataFrame.isnull() Method

DataFrame.isnull() function detect missing values in the given object. It return a boolean same-sized object indicating if the values are NA. Missing values gets mapped to True and non-missing value gets mapped to False.

Syntax: DataFrame.isnull()

Let’s create a pandas dataframe.

import numpy as np
import pandas as pd

students = [('Ankit', 22, 'Up', 'Geu'),
           ('Ankita', np.nan, 'Delhi', np.nan),
           ('Rahul', 16, 'Tokyo', 'Abes'),
           ('Simran', 41, 'Delhi', 'Gehu'),
           ('Shaurya', np.nan, 'Delhi', 'Geu'),
           ('Shivangi', 35, 'Mumbai', np.nan ),
           ('Swapnil', 35, np.nan, 'Geu'),
           (np.nan, 35, 'Uk', 'Geu'),
           ('Jeet', 35, 'Guj', 'Gehu'),
           (np.nan, np.nan, np.nan, np.nan)]

details = pd.DataFrame(students, columns =['Name', 'Age','Place', 'College'],
                        index =['a', 'b', 'c', 'd', 'e','f', 'g', 'i', 'j', 'k'])

print(details)

Output
       Name   Age   Place College
a     Ankit  22.0      Up     Geu
b    Ankita   NaN   Delhi     NaN
c     Rahul  16.0   Tokyo    Abes
d    Simran  41.0   Delhi    Gehu
e   Shaurya   NaN   Delhi     ...

Example 1 : Count total NaN at each column in DataFrame using isnull() method

import numpy as np
import pandas as pd

students = [
    ('Ankit', 22, 'Up', 'Geu'),
    ('Ankita', np.nan, 'Delhi', np.nan),
    ('Rahul', 16, 'Tokyo', 'Abes'),
    ('Simran', 41, 'Delhi', 'Gehu'),
    ('Shaurya', np.nan, 'Delhi', 'Geu'),
    ('Shivangi', 35, 'Mumbai', np.nan),
    ('Swapnil', 35, np.nan, 'Geu'),
    (np.nan, 35, 'Uk', 'Geu'),
    ('Jeet', 35, 'Guj', 'Gehu'),
    (np.nan, np.nan, np.nan, np.nan)
]

details = pd.DataFrame(
    students,
    columns=['Name', 'Age', 'Place', 'College'],
    index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k']
)

print("Boolean DataFrame:\n", details.isnull())
print("\nCount of NaN in each column:\n", details.isnull().sum())

Output
Boolean DataFrame:
     Name    Age  Place  College
a  False  False  False    False
b  False   True  False     True
c  False  False  False    False
d  False  False  False    False
e  False   True  Fal...

2. Dataframe.sum() Method

Dataframe.sum() function return the sum of the values for the requested axis. If the input is index axis then it adds all the values in a column and repeats the same for all the columns and returns a series containing the sum of all the values in each column.

Syntax: DataFrame.sum()

Example 2 : Count total NaN in DataFrame using Dataframe.sum() method

import numpy as np
import pandas as pd

students = [
    ('Ankit', 22, 'Up', 'Geu'),
    ('Ankita', np.nan, 'Delhi', np.nan),
    ('Rahul', 16, 'Tokyo', 'Abes'),
    ('Simran', 41, 'Delhi', 'Gehu'),
    ('Shaurya', np.nan, 'Delhi', 'Geu'),
    ('Shivangi', 35, 'Mumbai', np.nan),
    ('Swapnil', 35, np.nan, 'Geu'),
    (np.nan, 35, 'Uk', 'Geu'),
    ('Jeet', 35, 'Guj', 'Gehu'),
    (np.nan, np.nan, np.nan, np.nan)
]

details = pd.DataFrame(
    students, 
    columns=['Name', 'Age', 'Place', 'College'], 
    index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k']
)

print("Boolean DataFrame:\n", details.isnull())
print("\nCount total NaN in the DataFrame:\n", details.isnull().sum().sum())

Output
Boolean DataFrame:
     Name    Age  Place  College
a  False  False  False    False
b  False   True  False     True
c  False  False  False    False
d  False  False  False    False
e  False   True  Fal...


Next Article

Similar Reads

three90RightbarBannerImg