Encode Categorical Columns
One-hot and label encoding basics.
Encode Categorical Columns is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Encoding Matters
Most models only understand numbers, yet your data is full of words like red or blue. Encoding turns those labels into numbers a model can learn from. 🔢
Nominal vs Ordinal
Some categories have order, like small, medium, large. Others, like city names, do not. This order decides which encoding is honest.
Label Encoding
Label encoding assigns each category an integer: red becomes 0, blue becomes 1. Simple, but it invents an order that may not be real.
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
codes = le.fit_transform(df['color'])The Fake-Order Trap
If blue is 1 and green is 2, a model may think green is greater than blue. For nominal data that false ranking can hurt accuracy.
One-Hot Encoding
One-hot encoding makes one new column per category, marked 1 or 0. No fake order, just clean yes-or-no flags.
get_dummies in pandas
The fastest one-hot in pandas is get_dummies. Hand it a column and it expands every category into its own 0/1 column.
dummies = pd.get_dummies(df['color'])Avoid the Dummy Trap
The columns are perfectly correlated, so drop one with drop_first. This keeps linear models stable and avoids redundant information.
pd.get_dummies(df['color'], drop_first=True)OneHotEncoder for Pipelines
For models, prefer scikit-learn's OneHotEncoder. It remembers the categories it learned, so train and test stay perfectly aligned.
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(handle_unknown='ignore')Encode Order on Purpose
When order is real, map it yourself so small to large becomes 1 to 3. An ordinal mapping keeps the meaning intact.
order = {'small': 1, 'medium': 2, 'large': 3}
df['size_num'] = df['size'].map(order)Beware High Cardinality
One-hot on a column with thousands of values explodes into thousands of columns. For high cardinality, consider grouping rare values first.
Handle Unseen Categories
New data may contain labels your encoder never saw. Plan for this so an unseen category does not crash your prediction step.
Quick Check
Your color column has no natural order. Which encoding avoids inventing a false ranking?
Recap: Encoding
You turned words into numbers: one-hot for unordered categories, ordinal maps for real order, and label encoding only when order does not matter. 🎉
Frequently asked questions
Is the “Encode Categorical Columns” lesson free?
Yes — the full text of “Encode Categorical Columns” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Encode Categorical Columns”?
One-hot and label encoding basics. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Encode Categorical Columns” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Bin Numbers Into Categories
- Encode Categorical Columns
- Scale and Normalize Numbers
- Build Features From Dates and Text