Created
November 4, 2022 14:43
-
-
Save muhammadanas0716/2d4b714ca2c27b54fa5e246f8fe21076 to your computer and use it in GitHub Desktop.
Use ColumnTransformer to apply different preprocessing to different columns in ONE go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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