Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save muhammadanas0716/2d4b714ca2c27b54fa5e246f8fe21076 to your computer and use it in GitHub Desktop.
Save muhammadanas0716/2d4b714ca2c27b54fa5e246f8fe21076 to your computer and use it in GitHub Desktop.
Use ColumnTransformer to apply different preprocessing to different columns in ONE go.
import pandas as pd
df = pd.read_csv('http://bit.ly/kaggletrain', nrows=6)
cols = ['Fare', 'Embarked', 'Sex', 'Age']
X = df[cols]
from sklearn.preprocessing import OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.compose import make_column_transformer
oneHotEncoder = OneHotEncoder()
imputer = SimpleImputer()
column_transformer = make_column_transformer(
(oneHotEncoder, ['Embarked', 'Sex']), # Apply OneHotEncoder to Embarked and Sex
(imputer, ['Age']), # Apply SimpleImputer to Age
remainder='passthrough') # Include remaining column (Fare) in the output
# Column order: Embarked (3 columns), Sex (2 columns), Age (1 column), Fare (1 column)
column_transformer.fit_transform(X)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment