Predictive Hacks

Impute Missing Values in Pandas

We will provide an example of how you can impute missing values in Pandas following the rules below:

  • If the variable is numeric then impute the missing values with the mean
  • If the variable is object dtype then impute the missing values with the mode
import pandas as pd
import numpy as np

df = pd.DataFrame({'id':list(range(10)),
for i in df.columns:
    if (df[i].dtype=='object'):
        df[i].fillna(df[i].mode()[0], inplace=True)
    elif (df[i].dtype=='float64' or df[i].dtype=='int64'):
        df[i].fillna(df[i].mean(), inplace=True)


Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore


Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.


Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s