How to Handle Missing Data in Datasets

Real-world data is rarely clean and complete. Whether caused by sensor failures, optional survey fields, system migration bugs, or human error, missing data is one of the most common challenges in data science.
Ignoring missing values or blindly filling them with zeros can severely distort your statistical calculations, introduce bias, and cause machine learning models to crash or produce inaccurate predictions.
This guide outlines a structured, step-by-step framework to identify, analyze, and resolve missing data using Python.

1. Step 1: Detect and Quantify Missing Data

Before taking action, you need to measure the extent of missingness in your dataset.
In Python, the Pandas library provides built-in methods to identify missing values (NaN or None):
Python

import pandas as pd

# Load dataset
df = pd.read_csv('dataset.csv')

# Count total missing values per column
missing_counts = df.isnull().sum()

# Calculate the percentage of missing values per column
missing_percentage = (df.isnull().sum() / len(df)) * 100
Visualizing missing data using libraries like missingno helps identify whether missingness is clustered across specific columns or rows:
Python

import missingno as msno
import matplotlib.pyplot as plt

# Generate a matrix plot showing missing data patterns
msno.matrix(df)
plt.show()

2. Step 2: Identify the Missingness Mechanism

The correct strategy for handling missing values depends on why the data is missing. Statistically, missing data falls into three distinct categories:
                  ┌───────────────────────────────┐
                  │   Missing Data Mechanisms     │
                  └───────────────┬───────────────┘
                                  │
       ┌──────────────────────────┼──────────────────────────┐
       ▼                          ▼                          ▼
┌─────────────┐            ┌─────────────┐            ┌─────────────┐
│    MCAR     │            │     MAR     │            │    MNAR     │
│ (Completely │            │ (Random     │            │(Not Random, │
│  Random)    │            │ Conditional)│            │ Systemic)   │
└─────────────┘            └─────────────┘            └─────────────┘
  1. Missing Completely at Random (MCAR): The probability of a value being missing is completely random and unrelated to any observed or unobserved data. Example: A lab sample tube accidentally drops and shatters.
  2. Missing at Random (MAR): Missingness is systematically related to other observed features in the dataset, but not to the missing value itself. Example: Survey data shows that men are statistically less likely to fill in an “income” field, but the missingness correlates with the recorded “gender” feature.
  3. Missing Not at Random (MNAR): Missingness depends directly on the unobserved value itself. Example: Individuals with very high or very low incomes deliberately skip entering their salary details.
Rule of Thumb: Simple imputation methods work well for MCAR and MAR data. However, MNAR requires domain expertise or creating explicit flags (e.g., adding an is_missing binary feature column).

3. Method 1: Deletion Techniques (Use with Caution)

Deletion involves removing rows or columns that contain missing data. While easy to implement, it can result in loss of valuable information.

1. Listwise Deletion (Complete-Case Analysis)

Removes any row that contains at least one missing value.
Python

# Drop rows where any column contains a missing value
df_clean = df.dropna()
  • When to use: Only when missing values are strictly MCAR and represent a tiny fraction ($<5\%$) of total rows.
  • Risk: Significantly reduces dataset size and introduces severe sample bias if data is MAR or MNAR.

2. Column Dropping

Removes an entire column feature from the dataset.
Python

# Drop columns with more than 50% missing values
df_clean = df.drop(columns=df.columns[df.isnull().mean() > 0.5])
  • When to use: When a non-critical feature is missing more than $50-60\%$ of its values.

4. Method 2: Simple Imputation Techniques

Imputation replaces missing values with estimated values derived from available data.

1. Numerical Feature Imputation

  • Mean Imputation: Fills missing entries with the column average. Best for normally distributed numeric variables.
  • Median Imputation: Fills missing entries with the column median. Best for skewed numerical distributions with strong outliers.
Python

from sklearn.impute import SimpleImputer
import numpy as np

# Apply median imputation to numerical features
imputer_num = SimpleImputer(strategy='median')
df['age'] = imputer_num.fit_transform(df[['age']])

2. Categorical Feature Imputation

  • Mode Imputation: Replaces missing entries with the most frequent value.
  • Constant Category: Replaces NaN with a new category string like "Unknown" or "Missing".
Python

# Impute categorical column with a constant indicator
imputer_cat = SimpleImputer(strategy='constant', fill_value='Unknown')
df['city'] = imputer_cat.fit_transform(df[['city']])

5. Method 3: Advanced Machine Learning Imputation

When data is complex or heavily correlated, simple statistical metrics (mean/median) destroy underlying feature relationships. Advanced imputation algorithms predict missing values using other features.

1. K-Nearest Neighbors (KNN) Imputation

Replaces missing values using the weighted average of the $k$ most similar data points (neighbors) in the feature space.
Python

from sklearn.impute import KNNImputer

# Initialize KNN Imputer using 5 nearest neighbors
knn_imputer = KNNImputer(n_neighbors=5)
df_imputed = pd.DataFrame(knn_imputer.fit_transform(df), columns=df.columns)

2. Iterative Imputation (MICE Algorithm)

Iterative Impute (Multivariate Imputation by Chained Equations) models each feature with missing values as a function of all other features in a round-robin fashion.
Python

from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer

# Iteratively model each feature to impute values
mice_imputer = IterativeImputer(max_iter=10, random_state=42)
df_imputed = pd.DataFrame(mice_imputer.fit_transform(df), columns=df.columns)

Summary Matrix: Choosing the Right Strategy

Missingness Level Feature Type Recommended Strategy Python Tool
$< 5\%$ Missing Any Listwise row deletion df.dropna()
$< 20\%$ Missing Numerical (Skewed) Median Imputation SimpleImputer(strategy='median')
$< 20\%$ Missing Categorical Mode / Constant Imputation SimpleImputer(fill_value='Unknown')
Complex Correlations Mixed Numerical KNN or MICE Imputation KNNImputer / IterativeImputer
$> 50\%$ Missing Non-Target Column Drop the column df.drop(columns=[...])

Key Takeaway

Always evaluate how missingness occurs before deciding on a fix. For modern machine learning pipelines, aim to test models against both simple median imputations and advanced methods like KNN/MICE using cross-validation to ensure your choice improves downstream predictive performance.

About Adi Status

Adi Satus is a passionate financial writer with a keen interest in the ever-evolving world of loans, insurance, technology, and cryptocurrency. With years of experience researching and writing on a broad range of financial topics, Hindi Me Gyaan aims to simplify complex concepts and make them accessible for readers. Whether you're looking to secure a loan, navigate the world of insurance, explore the latest tech trends, or understand the intricacies of cryptocurrency, Hindi Me Gyaan provides expert insights and practical advice to help you make informed decisions. Always staying updated with the latest developments, Hindi Me Gyaan is dedicated to bringing you the most relevant, timely, and useful information to guide you on your financial journey.

View all posts by Adi Status →

Leave a Reply

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