0Pricing
Data Science Academy · Lesson

ColumnTransformer for Mixed Types

Different prep per column group.

ColumnTransformer for Mixed Types is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Columns Are Not Equal

Real tables mix numbers and categories, and each kind needs different prep. One scaler for everything simply will not work. 🧩

Meet ColumnTransformer

A ColumnTransformer applies different transformers to different column groups, all in one step you can drop inside a pipeline.

List Your Column Groups

First decide which columns are numeric and which are categorical. You will hand each group its own transformer by name.

num_cols = ['age', 'income']
cat_cols = ['city', 'plan']

Scale the Numbers

Numeric columns usually want a StandardScaler so large and small values share a fair footing before modeling.

from sklearn.preprocessing import StandardScaler

Encode the Categories

Categorical columns need numbers, so reach for OneHotEncoder to turn each category into its own 0/1 column.

from sklearn.preprocessing import OneHotEncoder

Wire It Together

Each entry is a tuple of name, transformer, and the columns it touches. The ColumnTransformer runs them side by side.

from sklearn.compose import ColumnTransformer
prep = ColumnTransformer([('num', StandardScaler(), num_cols), ('cat', OneHotEncoder(), cat_cols)])

Slot It Into a Pipeline

Place the transformer as the first step and a model as the last. Now mixed-type prep and modeling live in one pipeline.

pipe = Pipeline([('prep', prep), ('model', LogisticRegression())])

Leftover Columns

Columns you did not list are dropped by default. Set remainder to passthrough if you want to keep the rest untouched.

ColumnTransformer([...], remainder='passthrough')

Handle Unseen Categories

Test data may contain a category training never saw. Set handle_unknown to ignore so the encoder stays calm instead of crashing.

OneHotEncoder(handle_unknown='ignore')

Select Columns by Type

Tired of typing column names? make_column_selector grabs columns by dtype so your prep adapts as the table changes.

from sklearn.compose import make_column_selector as selector

One Fit, Both Paths

Calling fit once trains the scaler and the encoder together on the right columns. Each path learns only from its own columns.

Quick Check

You have numeric and categorical columns needing different prep. What handles that?

Recap

You can now route numeric and categorical columns to their own transformers with a ColumnTransformer inside one pipeline. Next, tuning. 🔧

Frequently asked questions

Is the “ColumnTransformer for Mixed Types” lesson free?

Yes — the full text of “ColumnTransformer for Mixed Types” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “ColumnTransformer for Mixed Types”?

Different prep per column group. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ColumnTransformer for Mixed Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Pipelines Beat Manual Steps
  2. ColumnTransformer for Mixed Types
  3. Tune With GridSearchCV
  4. Save and Reload a Trained Pipeline
← Back to Data Science Academy