Menu

Handling Missing Values

Handling Missing Values

Missing values are gaps in your dataset where data was not recorded, and you need to handle them before training a model, since most algorithms cannot work with empty values.

Common ways to handle missing values

  • Remove rows or columns with too many missing values.
  • Fill missing values with the mean, median, or mode.
  • Fill missing values using a fixed value or a prediction-based approach.

Checking for missing values

import pandas as pd

df = pd.read_csv("data.csv")

print(df.isnull().sum())

This shows how many missing values exist in each column, which helps you decide how to handle them.

Removing missing values

df = df.dropna()

dropna() removes any row that has at least one missing value, useful when missing data is very small in amount.

Filling missing values with mean

df["Age"] = df["Age"].fillna(df["Age"].mean())

This replaces missing values in the Age column with the average age, which keeps the dataset complete without losing rows.

Filling missing values with mode

df["City"] = df["City"].fillna(df["City"].mode()[0])

This fills missing categorical values with the most frequently occurring value in that column.

Handling missing values properly prevents training errors and keeps the dataset's overall pattern intact.


Machine Learning Tutorial

AP

Abhishek Pati



Learn via Video Course

Artificial Intelligence and Machine Learning Course Logo

Artificial Intelligence and Machine Learning Course

55 hrs



Get in Touch For Details