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):
Visualizing missing data using libraries like missingno helps identify whether missingness is clustered across specific columns or rows:
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 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.
-
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.
-
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.
-
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.
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.
2. Categorical Feature Imputation
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.
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.
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.