All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 4 · about 15 minutes · uses Lesson 4.1. You’ll be able to group numbers into bands and summarise by band.

cut puts numbers into bands with edges you choose; qcut makes bands that each hold about the same number of rows.

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

Pythonproducts.csv

The result is an ordered category, so bands sort low to high. observed=True leaves out empty bands.

2. qcut: equal-sized groups

Pythonorders.csv

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:

Prices. 4.5, 10, 18, 49, 120.
Edges. pd.cut(prices, [0, 10, 50, 1000], labels=['budget', 'mid', 'premium'])
10. Falls in (0, 10]: budget.
Result.
priceband
4.5budget
10budget
18mid
49mid
120premium

3. Your turn

Your turncustomers.csv
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

  1. Split order revenue into deciles.
  2. Edges [0, 18, 35, 65, 120] for ages: which bin is age 35 in?
  3. Why might qcut fail with “Bin edges must be unique”?
Answers
  1. pd.qcut(revenue, 10, labels=False) (0 to 9) or with labels.
  2. (18, 35], because the right edge is included.
  3. 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

1Which makes four groups with the same number of customers each?
2pd.cut(price, [0, 10, 20, 100]) puts a price of exactly 10 in which bin?
3Why use labels= in cut?

Sources

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.