Sub topic 12
Choosing between cut and qcut
This chapter assumes you can call pd.cut and pd.qcut and know what bin edges are. It covers how to decide which function fits a task.
The core difference
Both functions turn numbers into labeled ranges. They differ in what they hold fixed.
pd.cutfixes the edges. You pick them, or pandas spaces them evenly. The number of rows in each bin is whatever the data gives.pd.qcutfixes the row share. pandas computes the edges from quantiles, so each bin holds about the same number of rows. The width of each bin is whatever the data gives.
| Question | `pd.cut` | `pd.qcut` |
|---|---|---|
| What stays constant? | Bin boundaries | Rows per bin |
| Bin widths | Equal, or as you set them | Vary with the data |
| Bin counts | Vary, and bins can be empty | Nearly equal |
| Edges are meaningful in the real world | Yes | Usually not |
| Sensitive to outliers | Yes, with integer `bins` | Much less |
See it on skewed data
A few large values show the contrast clearly.
import pandas as pd
incomes = pd.Series([20, 22, 25, 27, 30, 35, 40, 60, 150, 400])
by_width = pd.cut(incomes, bins=3)
by_rank = pd.qcut(incomes, q=3)
print(by_width.value_counts().sort_index().tolist())
print(by_rank.value_counts().sort_index().tolist())The first line prints [8, 1, 1]. The range from 20 to 400 is split into three equal widths, so eight values land in the first bin. The second line prints [4, 3, 3]. The quantile edges follow the data, so the bins stay balanced.
Neither result is wrong. If you wanted groups of similar size, cut gave you a poor split. If you wanted fixed income brackets, qcut would give you edges that nobody chose.
A decision guide
Choose pd.cut when:
- The boundaries come from a rule, such as age brackets, tax bands, or grading cutoffs.
- You need to compare bins across datasets or over time. Fixed edges stay comparable, while quantile edges change with each dataset.
- You want to keep empty bins visible.
Choose pd.qcut when:
- You want rankings such as quartiles, deciles, or "top 10%".
- The data is skewed and equal-width bins would put most rows in one bin.
- You need balanced groups, for example to compare outcomes across tiers of similar size.
Failure modes to watch
cutwith an integer on skewed data. One outlier stretches the range, and most rows collapse into one bin. Use explicit edges or switch toqcut.qcutwith many repeated values. Equal quantile edges raise aValueError. Passduplicates="drop", which gives you fewer bins than requested.- Assuming
qcutbins are exactly equal. Ties and row counts that don't divide evenly mean sizes can differ by a few rows. - Reusing
qcutedges on new data. Recomputing quantiles on a new batch gives different edges. To apply the same boundaries again, save the edges withretbins=Trueand pass them topd.cut.
import pandas as pd
train = pd.Series([3, 8, 12, 20, 31, 45, 60, 88])
new = pd.Series([5, 25, 70])
_, edges = pd.qcut(train, q=4, retbins=True)
print(pd.cut(new, bins=edges, include_lowest=True))This learns the quartile edges from train, then applies the same boundaries to new. include_lowest=True keeps a value equal to the minimum edge from becoming NaN. A value outside the training range would still become NaN.
Exercise
A retailer wants to label customers by spending tier. Management has already decided that "low" means under 100, "mid" means 100 to 500, and "high" means above 500. Which function fits, and why?
Show answer
pd.cut with explicit edges, for example bins=[0, 100, 500, float("inf")]. The boundaries have a fixed meaning set by management, and they need to stay the same across months. pd.qcut would compute edges from each month's data and would not match the agreed thresholds.