Module 4: Grouping and aggregating · Lesson 4.5 · about 15 min
Bins and bands with cut and qcut
Turn numbers into bands: pd.cut for fixed edges you choose (price bands, age groups), pd.qcut for equal-sized groups (quartiles, deciles), then summarise by band.
Module 4 · about 15 minutes · uses Lesson 4.1. You’ll be able to group numbers into bands and summarise by band.
In everyday terms: postage: parcels go into weight bands (0-1 kg, 1-5 kg, over 5 kg) with fixed edges, like cut. A teacher splitting a class into four equal-sized reading groups by score is qcut.
1. cut: your own edges
The result is an ordered category, so bands sort low to high. observed=True leaves out empty bands.
2. qcut: equal-sized groups
Quartiles and deciles of customer spend are the starting point for segmentation (Lesson 8.4): how much revenue comes from the top quarter?
- pd.cut(x, bins=[0, 10, 50, 1000])
- Bands with your edges; (10, 50] means above 10 up to 50.
- labels=[...]
- Names for the bands.
- pd.qcut(x, 4)
- Quartile bands: about a quarter of rows each.
- right=False
- Include the left edge instead: [10, 50).
- Physical meaning
- Bands turn a continuous number into a category you can group and chart. cut keeps the meaning of the edges (price points); qcut keeps the group sizes equal (fair comparisons, RFM scores).
“A value on the edge goes in the higher band.” With the default right=True, 10 falls in (0, 10], the lower band. Check the edges against what the business means by “under $10”.
Watch it happen, one step at a time:
pd.cut(prices, [0, 10, 50, 1000], labels=['budget', 'mid', 'premium'])| price | band |
|---|---|
| 4.5 | budget |
| 10 | budget |
| 18 | mid |
| 49 | mid |
| 120 | premium |
3. Your turn
How it works
Principle: cut asks “which range does this value fall in?” with edges you set; qcut first finds the quantiles (Statistics Lesson 1.5) and uses them as edges, so each bin holds about the same number of rows.
When to use which: cut for bands that mean something to the business (price points, age groups, delivery distance); qcut for relative groups (top 25 % of spenders). With skewed data, equal-width cut(x, 4) puts nearly everyone in the first bin.
4. Practice
- Split order revenue into deciles.
- Edges
[0, 18, 35, 65, 120]for ages: which bin is age 35 in? - Why might
qcutfail with “Bin edges must be unique”?
Answers
pd.qcut(revenue, 10, labels=False)(0 to 9) or with labels.- (18, 35], because the right edge is included.
- Many repeated values (such as lots of zeros) make two quantiles equal. Use
duplicates='drop'or rank first.
Try it on your own data: histogram builder shows how your numbers spread before you choose edges.
New words: bin, cut, qcut, quantile bins, band, observed.
- cut: fixed edges you choose; qcut: equal-sized groups.
- Default bands include the right edge: (a, b].
- Name the bands with labels=.
- Group by the band to compare segments.
Check yourself
Sources
- pandas documentation, User Guide: Reshaping (cut, qcut) https://pandas.pydata.org/docs/user_guide/reshaping.html
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.