Skip to content
Tutorials Center
My learning

Topics

Lesson 59 of 111 ยท Feature Engineering and Pipelines

ColumnTransformer and Mixed Data

Apply different preprocessing to numeric and categorical columns with ColumnTransformer: selectors, remainder, pandas output, feature names and a full pipeline.

Tabular data is mixed. One column holds ages, another holds cities, a third holds a plan name, and a fourth has gaps. Each kind needs different treatment: numbers get imputed and scaled, categories get imputed and one-hot encoded. A plain pipeline applies the same steps to every column, so it cannot do this alone.

ColumnTransformer applies different transformers to different groups of columns and joins the results side by side. Put it as the first step of a pipeline, followed by a model, and you have a complete, leak-free tabular workflow in a single object. This lesson shows how to select columns, what happens to the ones you do not mention, how to get a DataFrame and readable column names back, and ends with a full example.

One table, two treatments

Numeric branch Impute the median, then scale.
Categorical branch Impute the most frequent value, then one-hot encode.
Concatenate Branch outputs are placed side by side.

Then the model sees one numeric matrix.

1 / 2

The basic form

ColumnTransformer takes a list of (name, transformer, columns) triples. The columns entry can be a list of names, positions, a slice, a boolean mask or a selector function. Each branch is fitted on its columns only, and the outputs are concatenated in the order of the list.

Python Two branches, one output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result has five columns: two scaled numbers and three one-hot columns, one for each city seen in training. The first branch's columns come first. Note that the one-hot encoder returns a sparse matrix when it is the only branch, but ColumnTransformer decides between dense and sparse depending on how much of the output is sparse (sparse_threshold, default 0.3). Here the result was dense enough to be a NumPy array.

Choosing columns with selectors

Listing columns by hand does not scale to 80 columns. make_column_selector returns a function that picks columns by dtype or by name pattern when the transformer is fitted. That also means the pipeline adapts when the table gets a new numeric column.

Python
from sklearn.compose import make_column_selector as selector

selector(dtype_include="number")      # all numeric columns
selector(dtype_exclude="number")      # everything else
selector(pattern="^price_")           # names matching a regular expression

Be careful with columns that are numbers but behave as categories (zip codes, IDs, a "month" column). Their dtype says number, so a dtype selector would scale them. List such columns explicitly.

What happens to the other columns

Columns you do not mention in any branch are controlled by remainder. The default is "drop": they vanish from the output. "passthrough" appends them unchanged, and you may also pass a transformer to treat all leftovers alike.

Python remainder and column selectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first transformer scaled the 0/1 column vip because its dtype is numeric. Often that is harmless, but if you want a column left alone, name the columns explicitly and let remainder="passthrough" carry the others through. The remainder__ prefix in the feature names marks passthrough columns.

Getting a DataFrame back

By default a transformer returns a NumPy array, and the column names are gone. set_output(transform="pandas") makes transformers return DataFrames with the right names, which helps with debugging and with models that care about names. You can call it on a single transformer, on a ColumnTransformer or on a whole pipeline.

Python set_output and get_feature_names_out

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

By default names carry the branch prefix (num__age, cat__city_Oslo). That keeps names unique when two branches produce the same name. When you are sure they cannot clash, verbose_feature_names_out=False gives the short form shown above.

A full tabular pipeline

Now we combine everything on a customer table with gaps. The numeric branch imputes the median and scales; the categorical branch imputes the most frequent value and one-hot encodes, ignoring categories it has never seen (handle_unknown="ignore"). A logistic regression sits at the end. The whole object is cross-validated as one unit, so imputer medians, scaler statistics and the category list are learned on training folds only.

flowchart LR
    D["DataFrame"] --> N["numeric columns<br/>impute, scale"]
    D --> K["categorical columns<br/>impute, one-hot"]
    N --> J["concatenate"]
    K --> J
    J --> M["LogisticRegression"]

Two branches run on different columns of the same table and meet before the model.

Python Mixed data, missing values, one pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The pipeline takes the raw DataFrame, gaps included, and the coefficients carry readable names, so you can see which direction each feature pushes the prediction. Pass a new customer with a city that was never seen in training and handle_unknown="ignore" encodes it as all zeros instead of raising an error at prediction time.

When a pipeline misbehaves, look inside: model[:-1].transform(X_te.head()) shows exactly what the classifier receives. With set_output(transform="pandas") on the preprocessing steps, that output is a labelled DataFrame.

Tuning branches

Parameter names follow the nesting: prep__num__simpleimputer__strategy is the strategy of the imputer inside the pipeline named num inside the ColumnTransformer named prep. Use model.get_params().keys() to list valid paths and search them with GridSearchCV as in Pipelines.

Python
grid = {
    "prep__num__simpleimputer__strategy": ["median", "mean"],
    "clf__C": [0.1, 1.0, 10.0],
}

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does ColumnTransformer do?

    It routes column groups to different transformers and concatenates the results side by side.
  2. 2. A column is not mentioned in any branch and remainder is left at its default. What happens to it?

    The default is remainder='drop'. Use remainder='passthrough' to keep unmentioned columns unchanged.
  3. 3. Why can a dtype-based selector be risky for a 'zip_code' column stored as an integer?

    dtype_include='number' picks every numeric column, including identifiers and codes that are really categories. List such columns by name.
  4. 4. What does OneHotEncoder(handle_unknown='ignore') do at prediction time for a category not seen in training?

    An unseen category gets zeros in every one-hot column, so prediction continues instead of failing.
  5. 5. Which call returns the readable names of the columns produced by a fitted ColumnTransformer?

    get_feature_names_out lists the output names, with branch prefixes unless verbose_feature_names_out is False. feature_names_in_ lists the input names.

Summary

  • ColumnTransformer sends column groups to different transformers and concatenates the outputs.
  • Choose columns by name, position, mask or make_column_selector (by dtype or name pattern); name numeric codes explicitly.
  • remainder decides the fate of unmentioned columns: "drop" (default) or "passthrough".
  • set_output(transform="pandas") and get_feature_names_out() keep the columns labelled.
  • Put the ColumnTransformer first in a pipeline, with a model last, and cross-validate the whole thing.

Next: Custom Transformers and Estimators.