How can I one hot encode in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
One-hot encoding converts a categorical value into a vector of zeros and ones so machine-learning models can work with category membership numerically. In Python, the two most common tools are pandas.get_dummies for dataframe-oriented work and scikit-learn's OneHotEncoder for training pipelines. Which one you use depends mostly on whether you are doing quick data transformation or building a reusable model pipeline.
Quick Encoding With Pandas
For exploration and simple preprocessing, pd.get_dummies is usually the fastest path.
This produces columns such as color_blue, color_green, and color_red, with 1 marking the active category.
You can also encode multiple dataframe columns:
This is excellent for quick analysis and feature preparation inside pandas workflows.
Production Pipelines With Scikit-Learn
If the encoded categories must stay consistent between training and inference, OneHotEncoder is usually the better choice.
The important advantage is that the encoder remembers the fitted categories. That makes it much safer when you deploy a model later.
Why the Choice Matters
get_dummies is simple, but it is a one-shot transformation. If your training set contains categories red, blue, and green, but the inference data later contains yellow, you must manage that mismatch yourself.
OneHotEncoder gives you a reusable fitted object with behavior for unseen categories, which is why it is preferred in model-serving pipelines.
Handling Unknown Categories
For machine learning, unknown categories are one of the biggest practical issues. With scikit-learn, this is usually handled through:
That means new categories will not crash the transform step. They simply map to zeros for the known output columns.
This is not always statistically ideal, but it is often operationally safer than failing on live traffic.
One-Hot Encoding and Model Type
Not every model needs one-hot encoding. Tree-based models can often work directly with other category handling strategies, while linear models usually benefit from one-hot encoding.
So the real question is not only "how do I one-hot encode". It is also:
- do I need one-hot encoding for this model
- do I need a quick dataframe transformation or a reusable training artifact
Answer that first, then choose the tool.
Preserve Feature Names Deliberately
Encoded columns are only useful if downstream code can identify them clearly. With pandas, prefixes help keep columns readable. With scikit-learn, use get_feature_names_out() and persist the fitted encoder along with the model so training and inference agree on the exact column order and meaning.
Common Pitfalls
- Using
get_dummiesduring training and then forgetting to align inference columns later. - Failing on unseen categories because encoder behavior was not configured.
- One-hot encoding too early without deciding which model pipeline actually needs it.
- Forgetting that encoded output shape depends on the fitted categories.
- Mixing quick notebook preprocessing with production feature logic and creating training-serving skew.
Summary
- Use
pd.get_dummiesfor quick pandas-based transformations. - Use scikit-learn
OneHotEncoderfor reusable machine-learning pipelines. - Handle unknown categories deliberately instead of waiting for production failures.
- Choose the tool based on whether you need convenience or a fitted preprocessing artifact.
- Keep training and inference encoding logic consistent.

