Handling Missing Values Strategically in Scikit-Learn Pipelines
Master strategic imputation in Scikit-Learn. Learn to configure SimpleImputer, chain logic in ColumnTransformer, and build pipelines that handle NaNs gracefully.
Previously in this course, we explored how to use ColumnTransformer for Heterogeneous Data: A Practical Guide to apply distinct processing to different feature types. Today, we bridge that gap by focusing on the most common hurdle in real-world data: missing values.
Data cleaning is often treated as an afterthought, but in a production pipeline, how you handle NaN values is as critical as your choice of model. If you impute based on global statistics calculated from your entire dataset, you are leaking information from the test set into your training process. We will solve this by embedding imputation strategies directly into your Scikit-Learn pipelines.
Strategic Imputation from First Principles
At its core, imputation is about making an informed guess to fill gaps in your data. The goal is to maintain the statistical integrity of your features without introducing bias.
In production, you rarely use the same strategy for every column. You need a strategy tailored to the distribution and nature of the feature:
- Numerical Features (Normal): Mean imputation is often sufficient.
- Numerical Features (Skewed/Outliers): Median imputation is more robust.
- Categorical Features: Mode (most frequent) or a constant value like "Missing" is standard.
By using SimpleImputer within a Pipeline, you ensure that the imputation value is calculated only using training data and then applied to both training and test sets consistently.
Configuring SimpleImputer for Different Distributions
The SimpleImputer class is your primary tool. It's flexible enough to handle various strategies. Let’s look at how to configure it for different data distributions.
PYTHONfrom sklearn.impute import SimpleImputer import numpy as np # For normally distributed data, use the mean mean_imputer = SimpleImputer(strategy=CE9178">'mean') # For skewed data or data with outliers, use the median median_imputer = SimpleImputer(strategy=CE9178">'median') # For categorical data, use the most frequent value mode_imputer = SimpleImputer(strategy=CE9178">'most_frequent') # For cases where you want to treat CE9178">'Missing' as a new category constant_imputer = SimpleImputer(strategy=CE9178">'constant', fill_value=CE9178">'missing')
The strategy='constant' is particularly powerful. Instead of guessing a value, you explicitly tell the model that "missingness" is a piece of information itself.
Chaining Imputation in ColumnTransformer
Since we are building production-style pipelines, we shouldn't handle missing data in isolation. We must chain the SimpleImputer inside the ColumnTransformer. This ensures the imputation happens before any scaling or encoding, as most transformers in Scikit-Learn will throw an error if they encounter NaN.
Here is a practical example of a preprocessing pipeline for our project:
PYTHONfrom sklearn.compose import ColumnTransformer from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler, OneHotEncoder # Define features numeric_features = [CE9178">'age', CE9178">'income'] categorical_features = [CE9178">'city', CE9178">'education'] # Create sub-pipelines numeric_transformer = Pipeline(steps=[ (CE9178">'imputer', SimpleImputer(strategy=CE9178">'median')), (CE9178">'scaler', StandardScaler()) ]) categorical_transformer = Pipeline(steps=[ (CE9178">'imputer', SimpleImputer(strategy=CE9178">'constant', fill_value=CE9178">'unknown')), (CE9178">'encoder', OneHotEncoder(handle_unknown=CE9178">'ignore')) ]) # Combine into a master processor preprocessor = ColumnTransformer( transformers=[ (CE9178">'num', numeric_transformer, numeric_features), (CE9178">'cat', categorical_transformer, categorical_features) ])
By nesting the SimpleImputer inside the Pipeline objects, we guarantee that the median for the 'age' column is derived solely from the training folds during cross-validation. This is the cornerstone of Pipeline Architecture Essentials: Building Robust ML Systems.
Hands-on Exercise
Using the code structure above, modify your preprocessing pipeline to handle a specific case: imagine you have a column last_purchase_amount where NaN values actually represent users who have never purchased anything (i.e., the value is effectively 0).
- Initialize a
SimpleImputerwithstrategy='constant'andfill_value=0. - Integrate this into the
numeric_transformerpipeline instead of the median imputer. - Verify that your pipeline correctly processes a test sample containing
NaNin that column.
Common Pitfalls
- Global Imputation: Never calculate the mean or median on your entire dataset before splitting. Always let the
Pipelinehandle it duringfit(). - Imputing Target Variables: Never impute missing target variables (labels). If your target is missing, you must drop those rows. Imputing labels creates "fake" ground truth that will destroy your model's predictive power.
- Ignoring Feature Types: Using
meanon a categorical feature (encoded as integers) will produce nonsensical values like 2.5 for a category that should be 1, 2, or 3. Always match the strategy to the feature type. - Forgetting to handle
NaNin Encoders: Even if you impute, check if yourOneHotEncoderneedshandle_unknown='ignore'to deal with categories that weren't present during training.
Recap
Handling missing data isn't just about filling gaps; it's about preserving the statistical relationship between features. By using SimpleImputer within ColumnTransformer and Pipeline, you create a reproducible, leak-proof workflow. You've now moved beyond basic data cleaning into a professional pattern for feature preparation.
Up next: We will discuss Scaling and Normalization Pipelines to ensure your features are on the same scale, preventing magnitude-based bias in your models.
Work with me

AI Automation & Agentic Workflow Development
Automate the repetitive work eating your time — content pipelines, data workflows, and agentic AI tasks that run themselves.

AI Chatbot & LLM Integration for Your App or Website
Add a smart AI chatbot or LLM feature to your product — trained on your content, integrated into your stack, and shipped by an AI-native engineer.