pandas in depth · lesson 9 of 12
Parts in this lesson
Apd.CategoricalDtype([...], ordered=True)a fixed list of values, in order
Bpd.cut(s, bins=[0, 20, 40, 60])numbers into bins you choose
Cpd.qcut(s, 3)three groups of equal size

Categories

Last updated

Some text columns only ever hold a few values: store, region, size. pandas has a type for them, category, which saves memory and, more usefully, can know the right order of its values. In this lesson you'll make ordered categories, see how much memory they save, and cut numbers into bins with cut and qcut.

By the end of this lesson you can

  • Make a column category, with an order that sorts and compares correctly
  • Measure the memory a category saves with memory_usage(deep=True)
  • Sort numbers into bins with pd.cut and into equal-sized groups with pd.qcut

Wrong way: small, medium, large

Sacks of rice come in three sizes. Sort them. Run it.

sizes.ipynb

large, medium, medium, small, small. Text sorts alphabetically, so large comes first and small last: the opposite of what anyone means. A chart of sales by size would put its bars in that order too, and a filter like sizes > "medium" would compare spellings, not sizes. Months (April before January) and days of the week have the same problem.

Ordered categories and memory

Run the first three cells.

categories.ipynb
  • pd.CategoricalDtype(["small", "medium", "large"], ordered=True) names the values a column may hold, in their real order. astype makes the column use it.
  • Now sorting follows that order: small, medium, large. And comparisons work: sizes > "small" is True for medium and large.
  • Any value not in the list becomes NaN, which also catches typos like Large early.
  • Memory: 100,000 store names take about 3.1 MB as text, and about 0.1 MB as a category, about 30 times less. A category stores each different value once, and a small whole-number code for each row. memory_usage(deep=True) counts the text itself, not just the pointers to it (lesson 2.1).

Use category for columns with few different values repeated over many rows. Don't use it for names, comments or IDs that are mostly different.

Cutting numbers into bins

Sometimes you want categories made from numbers: slow, normal and busy days. Step through, then look at the last two cells above.

Numbers falling into bins. Seven days of kilos. pd.cut sorts them into bins with edges you choose: 0 to 20, 20 to 40, 40 to 60.1229354251838kilos
1 of 3 Seven days of kilos. pd.cut sorts them into bins with edges you choose: 0 to 20, 20 to 40, 40 to 60.
  • pd.cut(kilos, bins=[0, 20, 40, 60], labels=[...]): bins with edges you choose. Each bin includes its right edge and not its left, written (20,40]: 40 is normal, 40.1 would be busy. Pick edges that mean something to the reader, and say what they are.
  • pd.qcut(kilos, 3, labels=[...]): bins chosen so each holds about the same number of values. The lowest third of days, the middle third, the top third. Good for "top 25% of stores"; bad when the edges need to mean something, because they move whenever the data does.

Both give an ordered category, so the bins sort and chart in order.

Check what you learned

A month column holds "Jan" to "Dec" as text. How will sort_values() order it?
Which column gains most from the category type?
pd.cut(s, bins=[0, 10, 20]). Which bin is 10 in?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis