Sub topic 9
Custom quantile cut points
This chapter assumes you can call pd.qcut with an integer q and that you know what a quantile is. Here you will pass q a list of fractions to place the cut points where you want them.
From a count to a list
When q is an integer, pd.qcut spaces the cut points evenly in probability. q=4 is shorthand for the list [0, 0.25, 0.5, 0.75, 1]. A list of fractions lets you choose uneven slices, such as the bottom 10%, the middle 80% and the top 10%.
import pandas as pd
scores = pd.Series(range(1, 101))
tiers = pd.qcut(
scores,
q=[0, 0.1, 0.9, 1.0],
labels=["bottom 10%", "middle 80%", "top 10%"],
)
print(tiers.value_counts().sort_index())The three bins hold 10, 80 and 10 values. Each fraction is converted to a value with the same method as Series.quantile. For these scores, the 0.1 quantile is 10.9 and the 0.9 quantile is 90.1.
Rules for the list
- Every fraction must be between 0 and 1.
- The fractions must be in increasing order.
- N fractions create N - 1 bins. If you pass
labels, give exactly N - 1 of them.
| Fractions passed | Bins created |
|---|---|
| `[0, 0.5, 1]` | 2 (below and above the median) |
| `[0, 0.1, 0.9, 1]` | 3 |
| `[0, 0.25, 0.5, 0.75, 1]` | 4 (same as `q=4`) |
Include 0 and 1 unless you want gaps
The first bin starts at your first fraction and the last bin ends at your last one. If you leave out 0 or 1, values beyond those quantiles fall outside every bin and become NaN.
import pandas as pd
scores = pd.Series(range(1, 101))
middle_only = pd.qcut(scores, q=[0.1, 0.9])
print(middle_only.isna().sum())This prints 20. The ten lowest and ten highest scores have no bin. Sometimes that is what you want, for example to separate out the middle of a distribution and ignore the tails. If it is not, include 0 and 1 so that every non-missing value gets a bin.
Check where the edges landed
Fractions describe the share of rows in each bin, not the values at the boundaries. To see the boundary values, add retbins=True.
import pandas as pd
scores = pd.Series(range(1, 101))
tiers, edges = pd.qcut(scores, q=[0, 0.1, 0.9, 1.0], retbins=True)
print(edges)The array is approximately [1.0, 10.9, 90.1, 100.0]. These edges depend on the data. If you later bin new data, a second call to qcut computes new edges. To reuse the first set of edges, pass them to pd.cut as bins.
Same limits as integer q
Custom fractions follow the same closure rules as the integer form. Bins are closed on the right, and the lowest bin includes the minimum value. Repeated values can still make two edges equal. In that case, duplicates="drop" removes the extra edges, and you end up with fewer bins than fractions. Then the number of labels no longer matches, so leave labels off or build it after you inspect the edges.
Exercise
You have a Series called scores. Write a pd.qcut call that labels the lowest 5% "low", the highest 5% "high", and everything else "typical", with no NaN values for valid scores.
Show answer
pd.qcut(scores, q=[0, 0.05, 0.95, 1.0], labels=["low", "typical", "high"]). The list starts at 0 and ends at 1, so every value is covered, and three labels match the three bins created by four fractions.