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.cutand into equal-sized groups withpd.qcut
Wrong way: small, medium, large
Sacks of rice come in three sizes. Sort them. Run it.
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.
pd.CategoricalDtype(["small", "medium", "large"], ordered=True)names the values a column may hold, in their real order.astypemakes the column use it.- Now sorting follows that order: small, medium, large. And comparisons work:
sizes > "small"isTruefor medium and large. - Any value not in the list becomes
NaN, which also catches typos likeLargeearly. - 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.
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 : 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
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.
- pandas: Categorical data: ordered categories, sorting and memory
- pandas: pandas.cut: bins, labels, and which edge is included
- pandas: pandas.qcut: bins by quantile