Data AnalyticsData Cleaningbeginner
Updated:

Categorical Data Encoding Basics

5 min read

Models need numbers, not text categories. Learn one-hot and label encoding in pandas, when each fits, and how to avoid the ordering trap, with examples.

TL;DR – Quick Answer

Categorical data encoding converts text categories into numbers so they can be used in models and some analyses. The two main methods are one-hot encoding, which creates a 0/1 column per category and suits unordered categories, and label encoding, which maps each category to an integer and suits ordered categories. In pandas, pd.get_dummies does one-hot encoding directly.

On This Page

Most machine learning models and many statistical tools only accept numbers, but real data is full of text categories: city, product type, subscription tier, colour. Categorical data encoding is the step that converts those text categories into numbers the tools can use. The catch is that how you convert matters — encode an unordered category the wrong way and you accidentally tell your model that "Chennai" is greater than "Bengaluru," which is meaningless. Choosing the right encoding is what keeps the numbers honest.

This topic assumes your categories are already clean; if the same category is still spelled several ways, fix that first with inconsistent category labels, or encoding will faithfully preserve the mess.

Ordered vs unordered categories

The single most important distinction:

  • Unordered (nominal) categories have no natural ranking — city, colour, payment method. No category is "more" than another.
  • Ordered (ordinal) categories do have a ranking — low < medium < high, bronze < silver < gold, education levels.

The encoding you pick must respect this. Ordered categories can become ordered integers; unordered ones must not, or you inject a fake ranking.

One-hot encoding

One-hot encoding creates a separate 0/1 column for each category. A row gets a 1 in the column for its category and 0 everywhere else. Because every category becomes its own independent column, no order is implied — which is exactly what unordered categories need. In pandas, pd.get_dummies does it directly.

import pandas as pd

df = pd.DataFrame({
    "customer": ["A", "B", "C", "D"],
    "city":     ["Hyderabad", "Pune", "Hyderabad", "Chennai"],
})

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

Expected output:

  customer  city_Chennai  city_Hyderabad  city_Pune
0        A         False            True      False
1        B         False           False       True
2        C         False            True      False
3        D          True           False      False

The single city column became three boolean columns, one per city, with exactly one True per row. No city is numerically "bigger" than another — the goal for nominal data. For linear models, add drop_first=True so one category is dropped and the columns are not perfectly redundant (the dummy variable trap):

encoded = pd.get_dummies(df, columns=["city"], drop_first=True)
print(list(encoded.columns))
['customer', 'city_Hyderabad', 'city_Pune']

Chennai is now the implicit baseline — a row that is 0 for both remaining columns is Chennai.

One-hot columns are also directly useful in plain analysis, not just modeling. Because each is a 0/1 indicator, taking its mean gives the proportion of rows in that category, and summing gives the count — a quick way to get category shares without a group-by.

Label encoding

Label encoding maps each category to an integer. This is appropriate only when the categories are genuinely ordered, and you should set the mapping deliberately so the numbers reflect the real order.

sizes = pd.DataFrame({"size": ["small", "large", "medium", "small", "large"]})

order = {"small": 0, "medium": 1, "large": 2}   # deliberate, correct order
sizes["size_code"] = sizes["size"].map(order)
print(sizes)
     size  size_code
0   small          0
1   large          2
2  medium          1
3   small          0
4   large          2

Now small < medium < large is preserved as 0 < 1 < 2, which is meaningful. Using map with an explicit dictionary is safer than an automatic encoder here, because it guarantees the order is the one you intend rather than alphabetical. A generic label encoder that assigns integers in alphabetical order would make large=0, medium=1, small=2 — the reverse of the real order — which is exactly the silent bug an explicit mapping avoids.

A quick decision guide

Faced with a categorical column, walk three questions. First, is it ordered? If yes, map it to ordered integers and you are done. If no, move on. Second, how many distinct values does it have? Check with df['col'].nunique(). A handful, one-hot encode it. Hundreds, and one-hot would explode your column count. Third, for high cardinality, reduce first: bucket rare categories into "Other", or keep only the top few and group the rest. This short routine handles the large majority of real encoding decisions without reaching for anything exotic.

Remember too that the target of a classification problem is itself categorical but is usually left as labels or label-encoded, not one-hot encoded, because most libraries expect a single target column. Encoding choices apply to the input features.

How analysts use it

In practice the decision tree is short: is the category ordered? If yes, map it to ordered integers. If no, one-hot encode it. Analysts also watch cardinality — the number of distinct categories. One-hot encoding a column with 500 unique values creates 500 columns, which bloats the data and can hurt models. For high-cardinality columns, common tactics are grouping rare values into an "Other" bucket first, or choosing a different encoding. Encoding is almost always one of the last cleaning steps, done just before modeling, after the categories themselves are clean and consistent.

Common mistakes

  • Label encoding unordered data. Turning city into 0,1,2 tells a model there is an order and distance between cities that does not exist. Use one-hot for nominal categories.
  • Ignoring the dummy variable trap. Keeping a column for every category creates perfect collinearity in linear models. Use drop_first=True.
  • One-hot encoding very high-cardinality columns. Hundreds of categories become hundreds of columns. Group rare ones or pick another method.
  • Encoding dirty categories. If "Hyderabad" and "hyderabad " are still separate, encoding creates two columns for one city. Standardize labels first.

In interviews

"What is the difference between one-hot and label encoding, and when would you use each?" is a standard question. The expected answer ties the choice to whether the category is ordered: one-hot for unordered to avoid implying rank, label (ordinal) for genuinely ordered categories. Mentioning pd.get_dummies, the drop_first option and the dummy variable trap signals hands-on experience, as does noting the high-cardinality problem. Interviewers want to see that you understand why imposing a false order is harmful, not just the two method names.

Where this fits in your learning path

Categorical encoding sits near the end of the cleaning-to-modeling handoff in the data cleaning cluster. It depends on clean categories from inconsistent category labels and often runs alongside normalization vs standardization when preparing features. Knowing how to encode categories correctly is a practical part of the data analyst roadmap.

Frequently Asked Questions

What is the difference between one-hot and label encoding?
One-hot encoding creates a separate 0/1 column for each category, so no false order is implied, making it right for unordered categories like city or colour. Label encoding assigns each category an integer, which implies an ordering, so it suits genuinely ordered categories like low/medium/high. Using label encoding on unordered data can mislead a model into seeing rank that does not exist.
How do I one-hot encode a column in pandas?
Use pd.get_dummies(df, columns=['col']). It replaces the text column with one boolean column per category. Add drop_first=True to drop one category and avoid redundancy, which is useful for linear models sensitive to collinearity.
When should I use label encoding?
Use label encoding only when the categories have a natural order you want to preserve, such as 'small' < 'medium' < 'large'. Map them deliberately so the integers reflect the correct order. For unordered categories, prefer one-hot encoding to avoid implying a false ranking.
What is the dummy variable trap?
The dummy variable trap is perfect collinearity that arises when you keep a one-hot column for every category, because any one is fully predictable from the others. Dropping one category with drop_first=True removes the redundancy and is standard practice for linear regression models.
Does encoding increase the number of columns?
One-hot encoding does: a column with 10 categories becomes 10 (or 9 with drop_first) columns. For very high-cardinality columns this can create too many features, in which case you group rare categories, use label encoding, or a more advanced technique. Label encoding keeps a single column.

Want to Build Your Career in Data Analytics with AI?

Join CodeBegun and train with working industry engineers — See the Data Analytics course in Hyderabad

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 16 July 2026 LinkedIn
Chat with us