Start studying
Module 7 · Data Cleaning Module contents
Module 7  ·  Card 4 / 13beginner

Inconsistent Categories

Inconsistent categories are different spellings of the same value in a categorical column.

For example, a country column might hold UK, U.K., United Kingdom and uk as 4 separate values.

A groupby on that column splits one country into 4 groups, each holding part of the true total.

The fix is a mapping that sends every variant to one agreed label.

Running value_counts on each text column is the quickest way to spot variants before they reach a report.

Learn in depth ↓
Module 07 · Data Cleaning · 13 concepts
In depth  /  15 lines

Inconsistent Categories, in more detail

A category column should hold a small, fixed set of labels, but free text entry and merged sources break that.

Each system or person spells the same thing slightly differently, and the computer treats every spelling as new.

The result looks like more categories than really exist, each holding part of the true count.

The first check is value_counts on the column, read from the rarest values upwards.

Rare values are often variants of common ones, such as Londn or Lodnon next to London.

The fix is a mapping from each variant to a standard label, kept in a file rather than buried in code.

For example, UK, U.K., uk, Great Britain and United Kingdom might all map to United Kingdom.

Keeping the mapping in a file means a new variant needs one new line, not a code change.

After mapping, the column can be stored with the pandas category type, which refuses later values outside the agreed set.

Some near matches are genuinely different, such as Austria and Australia, so automatic matching needs a human check.

Merging categories also loses detail, so the original column is often kept beside the cleaned one.

Categories drift as new products, regions or teams appear, so the mapping needs regular updates.

In practice the best fix is upstream, replacing a free text box with a fixed list in the form that collects the data.

Where that is impossible, a check that flags any label missing from the mapping catches new variants early.

The common mistake is fixing variants by hand in one report, so the next report shows the same split again.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
A groupby on the country column shows UK with 1,200 sales, United Kingdom with 800 and uk with 150. What is the right next step?
Data Cleaning

13 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close