Sitemap

Column Transformer in Machine Learning

1 min readJul 19, 2025

--

Column Tranfsformer is used in data preprocessing. It creates pipelines and transforms row in one go. Without it we need to extract the columns from dataframe , then transform it , and ultimately we need to further concatenate all such transformed columns ( which are now in form of numpy arrays) . Extracting , transforming , concatenating and converting into dataframe is a laborious task.

Get Animesh Tripathi’s stories in your inbox

Join Medium for free to get updates from this writer.

#import col transf class
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OrdinalEncoder,SimpleImputer
#create its instance
transformer = ColumnTransformer( transformers=[
('trnf1',OrdinalEncoder(categories([['bad','average','poor'],['school','UG','PG']])),['grade','education'])
('trnf2',SimpleImputer(),['Age'])
],remainders='passthrough')

transformer.fit_transform(X_train)
transformer.transform(X_test)

Step By Step Explaination

First we need to import classes that are to be used like column transformer , Ordinal encoder and simple imputer .
Then we create instance , there are two parameters used i.e. transformer and remainder , former tells which columns are to be tranformer and later shows what to do with leftover columns . Default value of remainder is ‘drop’ which drops the remaining columns . If we don’t want to drop and keep them as they are we give value ‘passthrough’ to the remainder.
Each value is a tuple in transformer , ‘tnfr1’ is just a label to that particular transformation , ‘OrdinalEncoding’ it tells which tranformation to apply , here it means categorical values are given numerical value based on order which they are written i.e. ‘bad’->0,’average’->1,’poor’->2 , and the last is the column name, for which whole story of tuple is written .

--

--

Animesh Tripathi
Animesh Tripathi

Written by Animesh Tripathi

Just sharing my thoughts with you all . And ofc , reading yours.