How to treat missing values in data in Python

Before machine learning algorithms can be used, some data pre-processing is required, missing value treatment is one of them.

How to find missing values in Python?

The function isnull() of a pandas data frame helps to find missing values in each column.

Sample Output

How to find missing values in Python
How to find missing values in Python


How to treat missing values?

Once you have found the missing values in each column, then you have below options

  • Deleting all the missing values
  • Replacing missing values with the median for continuous columns
  • Replacing missing values with the mode for categorical columns
  • Interpolating values


How to delete all missing values at once?

This option is exercised only if the number of rows containing missing values is much less than the total number of rows.

The dropna() function of the pandas data frame removes all those rows that contain at least one missing value.

Sample Output

Deleting all missing values from data in python
Deleting all missing values from data in python


Replacing missing values using median/mode

Missing values treatment is done separately for each column in data. If the column is continuous, then its missing values will be replaced by the median of the same column. If the column is categorical, then the missing values will be replaced by the mode of the same column.


Replacing missing values using interpolation

Instead of replacing all missing values with just one value(median/mode), you can also choose to interpolate the missing values based on the data present in the nearby location. This is called interpolation.

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

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