scikit-learn 1 · Preprocessing and pipelines

Treat columns differently

Scale numeric columns and one-hot encode category columns in one transformer.

import pandas as pd
from sklearn.compose import ColumnTransformer

df = pd.DataFrame({"age": [25, 32, 47], "city": ["Rome", "Oslo", "Rome"]})
preprocess = ColumnTransformer([
    ("num", StandardScaler(), ["age"]),
    ("cat", OneHotEncoder(handle_unknown="ignore"), ["city"]),
])
features = preprocess.fit_transform(df)

Official scikit-learn 1 documentation

More preprocessing and pipelines commands

All preprocessing and pipelines commands · scikit-learn 1 cheatsheet

Was this page helpful?