Check the null values from Pandas DataFrame in Python

In this tutorial, we are going to see how to find the null values from Pandas DataFrame in Python. Pandas DataFrame is a temporary table form of a given dataset.

First, import the pandas library

import pandas as pd

Read the data file using the read_csv(path) (according to a file format) function and create its data frame using DataFrame(data) function from the pandas library.

data = pd.read_csv('airquality.csv')
df = pd.DataFrame(data)
df

Output:

Check the null values from Pandas DataFrame in Python

isnull( ): function to check whether the value is null or not

isnull() is the method that returns true if the value is null and false otherwise. All the values from DataFrame get replaced with true or false.
Now use this function to find which values are null from DataFrame.

df.isnull()

Output:

isnull( ): function to check whether the value is null or not
Note: Similarly isna() is also one method that works in the same way as isnull()

 

The sum() method returns the sum of the values.

Now use sum( ) function to get a sum of null values from each column

df.isnull().sum()

Output:

sum( ) function to get a sum of null values from each column

This method is used while developing machine learning models. As null values affect the accuracy of the ML model. In this case, either these null values are removed or filled by that particular column mean.

Also, refer to Handling Missing Data Using Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *