Filling in missing values in Pandas

While we would love to obtain datasets that contain no missing values whatsoever, the reality is that we almost always have to handle them. This recipe shows you four methods that you can use.

Getting ready

Pandas provides a fillna() method to fill in missing values. Create a DataFrame from the customer data using the previous recipe, and then try each of the following methods.

How to do it…

The following code tells us how to fill in missing values in Pandas:

# 1: Replace all missing values with a string - 'Missing' customers.fillna('Missing', inplace=True) # 2: Replace all missing values with a 0 customers.fillna(0, inplace=True) # 3: Replace all missing values with the mean of the DataFrame customers.fillna(raw_data.mean(), ...

Get Python Business Intelligence Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.