Different Columns Need Different Work

Route by semantic type

Numeric columns may need imputation and scaling; categorical columns may need missing-value handling and one-hot encoding. A scikit-learn ColumnTransformer applies separate pipelines to declared column groups and concatenates their outputs in a stable order.

from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler

numeric = Pipeline([('impute', SimpleImputer(strategy='median')), ('scale', StandardScaler())])
categorical = Pipeline([('impute', SimpleImputer(strategy='most_frequent')),
                        ('encode', OneHotEncoder(handle_unknown='ignore'))])
prep = ColumnTransformer([('num', numeric, ['age','income']),
                          ('cat', categorical, ['plan','region'])])

Explicit column lists are a data contract. A missing required column should fail clearly; an unexpected column should not silently become a feature. Inspect get_feature_names_out() so transformed coordinates remain traceable.

Scenario: Production adds a new string column at the front of a CSV. Position-based preprocessing shifts every feature. Name-based selection either ignores it deliberately or fails the schema check before corrupting inference.
Tip: Treat semantic types separately from Pandas dtypes. Integer postal codes are categories; numeric strings may be measurements needing explicit coercion.