Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

ColumnTransformer

ColumnTransformer applies different transformers to selected columns of an array or pandas DataFrame and concatenates their outputs into one feature space. It is useful for heterogeneous data requiring separate preprocessing paths.

How it works

ColumnTransformer routes selected columns to separate transformers, fits each transformer on its assigned subset, and concatenates their outputs into one feature space. Unselected columns are dropped by default or can be passed through or processed by a remainder estimator.

Syntax

class sklearn.compose.ColumnTransformer(*transformers*, *, remainder='drop', sparse_threshold=0.3, n_jobs=None, transformer_weights=None, verbose=False, verbose_feature_names_out=True)

Parameters

NameTypeDescription
transformerslist of tuplesList of (name, transformer, columns) tuples specifying transformers and the input subsets they process.
remainder{'drop', 'passthrough'} or estimator, default='drop'Treatment for columns not selected by transformers: 'drop', 'passthrough', or an estimator.
sparse_thresholdfloat, default=0.3Output density threshold used to decide whether a sparse result remains sparse.
n_jobsint, default=NoneNumber of jobs used to fit and transform transformers in parallel; None uses one job.
transformer_weightsdict, default=NoneMultiplicative weights applied to the outputs of individual transformers.
verbosebool, default=FalseWhether to print progress messages during fitting and transforming.
verbose_feature_names_outbool or callable, default=TrueControls whether generated output feature names include transformer prefixes.

Returns

A fitted ColumnTransformer instance from fit; transform produces the concatenated transformed feature space.

Key points

  • It works with array inputs and pandas DataFrames.
  • Outputs from the configured transformers are joined horizontally into one transformed representation.
  • It is designed for datasets whose columns need different preprocessing or feature extraction.
  • The main configuration is a list of named transformer-and-column selections.
  • The remainder option can retain every otherwise-unselected input column.

Common mistakes

  • A transformer supplied for a column subset must implement both fit and transform.
  • Integer selectors address positions, whereas string selectors address DataFrame column names.
  • Use a scalar selector when the delegated transformer expects a one-dimensional input; otherwise it receives two dimensions.

Example: Transform heterogeneous columns

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder, StandardScaler

preprocessor = ColumnTransformer(
    transformers=[
        ("numeric", StandardScaler(), ["age", "income"]),
        ("categorical", OneHotEncoder(handle_unknown="ignore"), ["city"]),
    ],
    remainder="drop",
)
X_transformed = preprocessor.fit_transform(X)

This applies scaling and one-hot encoding to separate DataFrame columns, then concatenates the resulting feature blocks.

When to use it

Heterogeneous tabular preprocessing

A DataFrame mixes numeric and categorical fields that need different preprocessing.

Combining feature blocks

Several feature extraction steps must feed one estimator as a unified input.

Selective transformation

Only selected columns should be transformed while untouched columns remain available.

Interview questions

QWhat problem does ColumnTransformer solve?

The constructor receives named (name, transformer, columns) tuples. Each transformer handles its selected subset, and the outputs are concatenated into a single feature space.

Follow-up: How would you preserve columns not listed in the tuples? Set remainder='passthrough', or provide an estimator as remainder if those columns need additional processing.

QHow are unspecified columns handled?

The default remainder is 'drop', so unspecified columns are omitted. Setting remainder='passthrough' appends the remaining columns unchanged; an estimator can instead transform them.

Follow-up: What constraint applies to a remainder estimator? It must support both fit and transform, just like the explicitly configured transformers.

QWhich column selectors are supported?

The columns selector may be a name, integer, array-like selector, boolean mask, slice, or callable. A callable receives X and returns a valid selector.

Follow-up: How can selectors differ between arrays and DataFrames? Integer selectors are positional, while strings can refer to DataFrame column names.

  • sklearn.compose.make_column_transformer
  • sklearn.compose.make_column_selector
  • sklearn.pipeline.Pipeline
  • sklearn.preprocessing.StandardScaler
  • sklearn.preprocessing.OneHotEncoder

Source: ColumnTransformer — scikit-learn 1.9.1 documentation. Checked 2026-10-15.