Python Central HubSign in

Data Analytics with Python / Phase 4: Data Preprocessing and Cleaning

One-Hot Encoding

Convert categorical variables into 0/1 columns using one-hot encoding (Pandas and scikit-learn).

What is one-hot encoding?

One-hot encoding converts a categorical column into multiple binary columns.

Example:

Python
- city = {Pune, Delhi}

becomes:

  • city_Pune (0/1)
  • city_Delhi (0/1)

One-hot encoding with Pandas

Python
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:

Python
encoded = pd.get_dummies(df, columns=["city"], drop_first=True)
print(encoded)

One-hot encoding with scikit-learn

Python
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

diagram One-hot encoding mermaid
A single categorical column expands into one binary column per distinct category.
sketch One column becomes many binary columns p5.js
Each row keeps a single 1 marking which category it belongs to; every other column for that row is 0.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ One-hot encode with pandas

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Drop the first category

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ One-hot encode with scikit-learn

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. What does pd.get_dummies(df, columns=["city"]) create for cities Pune and Delhi?

    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.

  2. What does drop_first=True do?

    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.

  3. Why pass handle_unknown="ignore" to OneHotEncoder?

    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.

  4. What problem do high-cardinality categories cause with one-hot encoding?

    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.

Was this page helpful?

Let us know how we did