ColumnTransformer
noun
say it: “KOL-um trans-FOR-mer” /ˈkɑləm trænsˈfɔrmər/
A scikit-learn tool that applies different preparation steps to different columns of a table and joins the results.
What is ColumnTransformer?
ColumnTransformer prepares different columns in different ways and joins the results side by side, such as
StandardScaler on the number columns and OneHotEncoder on the category columns. Each part gets a name, a tool and
its columns; columns no part names are dropped unless remainder="passthrough".
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
pets = pd.DataFrame({"age": [1, 3, 5], "species": ["dog", "cat", "dog"]})
prep = ColumnTransformer([("num", StandardScaler(), ["age"]), ("cat", OneHotEncoder(), ["species"])])
print(prep.fit_transform(pets).round(2))
# [[-1.22 0. 1. ] age scaled, then species_cat, species_dog
# [ 0. 1. 0. ]
# [ 1.22 0. 1. ]]
In a sentence
- “The ColumnTransformer scaled the ages and one-hot encoded the species in one step.”
- “A ColumnTransformer inside the pipeline kept every column's preparation together.”
Level: Freelancer · More Python terms at this level
Updated