What is one-hot encoding?
One-hot encoding converts a categorical column into multiple binary columns.
Example:
- city = {Pune, Delhi}becomes:
- city_Pune (0/1)
- city_Delhi (0/1)
One-hot encoding with Pandas
import pandas as pd
df = pd.DataFrame({"city": ["Pune", "Delhi", "Pune"], "amount": [100, 200, 150]})
encoded = pd.get_dummies(df, columns=["city"], drop_first=False)
print(encoded)Avoid dummy variable trap (optional)
For some linear models, you can drop one category:
encoded = pd.get_dummies(df, columns=["city"], drop_first=True)
print(encoded)One-hot encoding with scikit-learn
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
X = pd.DataFrame({"city": ["Pune", "Delhi", "Pune"]})
enc = OneHotEncoder(sparse_output=False, handle_unknown="ignore")
arr = enc.fit_transform(X[["city"]])
print(arr)
print(enc.get_feature_names_out(["city"]))Tips
- High-cardinality categories (thousands of unique values) can explode feature count.
- Consider grouping rare categories into "Other".
Visualize it
flowchart LR A["city column
(Pune, Delhi, Pune)"] --> B["pd.get_dummies(df, columns=['city'])"] B --> C["city_Pune (0/1)"] B --> D["city_Delhi (0/1)"]
๐งช Try It Yourself
Exercise 1 โ One-hot encode with pandas
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Drop the first category
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ One-hot encode with scikit-learn
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Check your understanding
What does pd.get_dummies(df, columns=["city"]) create for cities Pune and Delhi?
One-hot encoding turns a categorical column into multiple binary columns, one per category.
Show answer
A โ One 0 or 1 column per city, such as city_Pune and city_Delhi โ One-hot encoding turns a categorical column into multiple binary columns, one per category.
What does drop_first=True do?
Dropping one column removes the redundancy, since the dropped category is implied when all others are 0.
Show answer
A โ Drops one category column to avoid the dummy variable trap in linear models โ Dropping one column removes the redundancy, since the dropped category is implied when all others are 0.
Why pass handle_unknown="ignore" to OneHotEncoder?
With handle_unknown=ignore, new categories at transform time are encoded as all zeros rather than raising an error.
Show answer
A โ A category unseen during fit gets all zeros instead of an error โ With handle_unknown=ignore, new categories at transform time are encoded as all zeros rather than raising an error.
What problem do high-cardinality categories cause with one-hot encoding?
Thousands of unique values mean thousands of columns. Consider grouping rare categories into Other.
Show answer
A โ The feature count explodes โ Thousands of unique values mean thousands of columns. Consider grouping rare categories into Other.
Next
For categories that do have a natural order, see Label Encoding instead of one-hot encoding.