8 Methods For Handling Missing Values With Python Pandas (2024)

8 Methods For Handling Missing Values With Python Pandas (3)

All the images were created by the author unless stated otherwise.

Missing values might be the most undesired values in data science. We definitely do not want to have them. However, they are always around.

Since it is not reasonable to ignore missing values, we need to find ways to handle them efficiently and properly.

Pandas, being one of the best data analysis and manipulation libraries, is quite flexible in handling missing values.

In this article, we will go over 8 different methods to make the missing values go away without causing a lot of trouble. Which method fits best to a particular situation depends on the data and task.

Let’s start by creating a sample data frame and adding some missing values to it.

8 Methods For Handling Missing Values With Python Pandas (4)
8 Methods For Handling Missing Values With Python Pandas (5)

We have a data frame with 10 rows and 6 columns.

The next step is to add the missing values. We will use the loc method to select the row and column combinations and make them equal to “np.nan” which is one of the standard missing value representations.

8 Methods For Handling Missing Values With Python Pandas (6)

Here is how the data frame looks now:

8 Methods For Handling Missing Values With Python Pandas (7)

The item and measure 1 columns had integer values but they have been upcasted to float because of the missing values.

With Pandas 1.0, an integer type missing value representation (<NA>) was introduced so we can have missing values in integer columns as well. However, we need to explicitly declare the data type.

8 Methods For Handling Missing Values With Python Pandas (2024)
Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 6283

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.