Python Pandas – Count number of NaN values in each column

In this post we will be creating a Python script that will count the number of Nan values in each column. Pandas is a Python library which is an amazing tool for data analysis and manipulation.

In this example we’ll retrieve our data from a CSV file, a sample of which can be seen below. (You can see some cells have no values.)

To load the CSV data in to a Pandas data frame we would use the snippet of code below.

import pandas as pd

df = pd.read_csv('Sample.csv')

No we will move on to counting the number of NaN values in each column, to do this we will use the ‘isna()’ method which will return true or false if there is NaN value present within a cell.

NaN_count = df .isna().sum()

The above would return the following as output.

Rank            0
Name            1
Platform        0
Year            2
Genre           0
Publisher       2
NA_Sales        0
EU_Sales        2
JP_Sales        0
Other_Sales     1
Global_Sales    1
dtype: int64

Leave a Reply