Lesson 11 of 13 · 2 min
Encoding Categorical Variables
import pandas as pd
# Option 1: map (binary/ordinal)
df['Gender'] = df['Gender'].map({'Male': 0, 'Female': 1})
# Option 2: get_dummies (one-hot encoding)
df = pd.get_dummies(df)
# Creates new binary columns for each category
# Option 3: LabelEncoder
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['City'] = le.fit_transform(df['City'])
