Categories: pandasPython

Handling Missing Values in Pandas

In a real-life scenario, we often come across datasets with missing values. However, we need to handle these missing values in order to perform Data Analysis or Machine Learning operations. Properly cleaned data makes it easier and more accurate to perform various functionalities.

Missing data is generally represented by null, None, or NaN.

In this article, we will look at various ways to detect, remove, or replaces data in Pandas. For this purpose let’s work on the following student_record dataset:

import numpy as np
import pandas as pd

student_records = [[‘John’,14,82.5],[‘Maria’,np.nan,90.0],[‘Tom’,13,77.0],[‘Amy’,np.nan,71.0]]

df = pd.DataFrame(student_records,columns=[‘Name’,’Age’,’Marks’])
print(df)

The DataFrame is:

Name Age Marks
0 John 14.0 82.5
1 Maria NaN 90.0
2 Tom 13.0 77.0
3 Amy NaN 71.0

We can see there are several NaN values in the DataFrame.

isnull()

isnull() function is used to check the DataFrame for missing values. It returns a DataFrame with Boolean values which are ‘True’ if the cell has NaN value.

For example, for the student_record DataFrame:

df.isnull()

This gives the following output:

Name Age Marks
0 False False False
1 False True False
2 False False False
3 False True False

We can also use this to display the rows which have null values:

df[df.isnull().any(1)]

Output:

Name Age Marks
1 Maria NaN 90.0
3 Amy NaN 71.0

notnull()

Similar to the isnull() function, notnull() function returns ‘True’ for the rows which do not have null values.

Eg:

df.notnull()

Output:

Name Age Marks
0 True True True
1 True False True
2 True True True
3 True False True

dropna()

dropna() will remove or drop the rows which contain null values.

Eg:

df.dropna()

Output:

Name Age Marks
0 John 14.0 82.5
2 Tom 13.0 77.0

We can also remove columns having null values using this function:

df.dropna(axis=1)

Output:

Name Marks
0 John 82.5
1 Maria 90.0
2 Tom 77.0
3 Amy 71.0

fillna()

This function is used to replace the missing value with some value.

Eg:

df.fillna(0)

This gives:

Name Age Marks
0 John 14.0 82.5
1 Maria 0.0 90.0
2 Tom 13.0 77.0
3 Amy 0.0 71.0

We can also fill the values using previous values or string values, eg:

df.fillna(method=’pad’)

Output:

Name Age Marks
0 John 14.0 82.5
1 Maria 14.0 90.0
2 Tom 13.0 77.0
3 Amy 13.0 71.0

replace()

replace() is used to replace the missing values using required values.

For example,

df.replace(to_replace = np.nan, value = 12)

This will replace the NaN values with 12.0

Name Age Marks
0 John 14.0 82.5
1 Maria 12.0 90.0
2 Tom 13.0 77.0
3 Amy 12.0 71.0

Summary

In this article, we looked at several ways to handle missing values in Pandas. In the upcoming article, our focus would be on Grouping data in Pandas.

Pallavi Pandey

Recent Posts

MapReduce Algorithm

In this tutorial, we will focus on MapReduce Algorithm, its working, example, Word Count Problem,…

8 months ago

Linear Programming using Pyomo

Learn how to use Pyomo Packare to solve linear programming problems. In recent years, with…

1 year ago

Networking and Professional Development for Machine Learning Careers in the USA

In today's rapidly evolving technological landscape, machine learning has emerged as a transformative discipline, revolutionizing…

1 year ago

Predicting Employee Churn in Python

Analyze employee churn, Why employees are leaving the company, and How to predict, who will…

2 years ago

Airflow Operators

Airflow operators are core components of any workflow defined in airflow. The operator represents a…

2 years ago

MLOps Tutorial

Machine Learning Operations (MLOps) is a multi-disciplinary field that combines machine learning and software development…

2 years ago