Skip to main content

How to One-Hot Encode a Column with OneHotEncoder in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Models do arithmetic, so a column of words such as red, green and blue has to become numbers first. One-hot encoding gives every word its own column, holding 1 where a row has that word and 0 elsewhere. scikit-learn's OneHotEncoder does it, and remembers the words it saw so new data is encoded the same way. This guide encodes a small column of colors, with real output from scikit-learn 1.9.1.

Syntax​

Syntax
encoder = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
encoded = encoder.fit_transform(frame[[column]])
PartWhat it means
handle_unknown="ignore"A category the encoder didn't see while fitting becomes all zeros instead of an error. The default, "error", raises one
sparse_output=FalseReturn a plain array. The default, True, returns a sparse matrix, which stores only the 1s: compact, but harder to read
fit_transform(...)Learns the categories, then encodes the data. Later data uses transform
frame[[column]]The column as a one-column table: double brackets. The encoder expects a table, not a single list

Step 1: Encode a Column​

Create one_hot.py:

The one_hot.py script in a macOS-style code window: it builds a DataFrame with a color column of red, green, blue and green, fits a OneHotEncoder with handle_unknown ignore and sparse_output False, prints the feature names and the encoded array, then encodes the unseen color purple.

one_hot.py
import pandas as pd
from sklearn.preprocessing import OneHotEncoder

colors = pd.DataFrame({"color": ["red", "green", "blue", "green"]})

encoder = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
encoded = encoder.fit_transform(colors)

print(encoder.get_feature_names_out())
print(encoded)
print(encoder.transform(pd.DataFrame({"color": ["purple"]})))
Terminal
uv run python one_hot.py
Output
['color_blue' 'color_green' 'color_red']
[[0. 0. 1.]
[0. 1. 0.]
[1. 0. 0.]
[0. 1. 0.]]
[[0. 0. 0.]]

A terminal running uv run python one_hot.py: the names color_blue, color_green and color_red, a four-row array with a single 1 in each row, and an all-zero row for purple.

Step 2: Read the Result​

  • Three categories, three columns, sorted alphabetically: color_blue, color_green, color_red.
  • Each row has a single 1, in its color's column: the first row is red, so [0. 0. 1.].
  • purple, which the encoder never saw, became [0. 0. 0.] thanks to handle_unknown="ignore". A model reads that as "none of the colors I know".

The encoder keeps its categories, so encode new data with transform, never fit_transform: fitting again would forget the old ones and could put the columns in a different order.

Common Mistakes​

MistakeWhat happensDo this instead
Keeping the default handle_unknown="error"New data with an unseen category fails: ValueError: Found unknown categories ['purple'] in column 0 during transformhandle_unknown="ignore", when unseen categories can happen
Printing the default output<Compressed Sparse Row sparse matrix of dtype 'float64' with 2 stored elements and shape (2, 2)> instead of numberssparse_output=False, or .toarray()
Fitting the encoder on new dataThe columns can change, and the model gets inputs it wasn't trained onFit once on training data, then transform

Troubleshooting​

SymptomLikely causeFix
ValueError: Found unknown categories [...] in column 0 during transformA category that wasn't in the training datahandle_unknown="ignore", or check the data
Too many columnsA column with hundreds of different values, such as IDsDon't one-hot encode IDs; for common-but-many categories, look at min_frequency or max_categories

FAQ​

How do I encode only some columns of a table?​

Wrap the encoder in a ColumnTransformer: How to Transform Only Some Columns with ColumnTransformer in scikit-learn.

Isn't pandas.get_dummies the same?​

It produces similar columns, but it doesn't remember the categories, so new data can come out with different columns. OneHotEncoder fits once and encodes everything after the same way, which is what a model needs.

Conclusion​

OneHotEncoder(handle_unknown="ignore", sparse_output=False) turned four colors into three 0/1 columns and an unseen color into all zeros. Fit it on training data, transform everything after, and read the column names with get_feature_names_out().

Additional Resources​

  • OneHotEncoder — its parameters, including handle_unknown and sparse_output.