Sub topic 2
Equal-width bins with pd.cut
This chapter assumes you can build a pandas Series and read basic statistics such as minimum and maximum. You will learn how pd.cut splits a numeric column into a fixed number of bins of equal width.
The idea
Pass an integer as bins, and pd.cut does three things:
- It finds the minimum and maximum of the data.
- It divides that range into
binsintervals of identical width. - It assigns each value to the interval that contains it.
A worked example
import pandas as pd
ages = pd.Series([3, 17, 25, 42, 67, 90])
groups, edges = pd.cut(ages, bins=3, retbins=True)
print(edges)
print(groups.value_counts().sort_index())The range is 90 - 3 = 87, so each bin is 29 units wide. The raw edges are 3, 32, 61 and 90. The printed edges are [2.913, 32., 61., 90.]. The lowest edge is lower than the minimum, and the next section explains why.
The counts are 3 values in the first bin, 1 in the second and 2 in the third. The values 3, 17 and 25 fall in (2.913, 32.0]. The value 42 falls in (32.0, 61.0]. The values 67 and 90 fall in (61.0, 90.0].
The 0.1% edge adjustment
By default intervals are closed on the right, written (a, b]. If the first interval started exactly at the minimum, the minimum itself would be excluded and become NaN. To prevent this, pd.cut moves the lowest edge down by 0.1% of the range (0.001 × 87 = 0.087). That is why the first edge is 2.913 and not 3.
This adjustment applies only when you pass an integer. With explicit edges, which the next chapter covers, you control the edges yourself.
Controlling how edges are displayed
The edges are often long decimals. The precision parameter sets how many decimal places appear in the interval labels. It changes the display only, not the actual edges.
import pandas as pd
ages = pd.Series([3, 17, 25, 42, 67, 90])
print(pd.cut(ages, bins=3, precision=0))Common errors
- Outliers stretch the bins. Equal width is computed from the minimum and maximum. If one value is 10,000 and the rest are below 100, nearly every row lands in the first bin and the other bins are empty. Inspect
describe()before choosingbins=n, and consider explicit edges orpd.qcutif the data is skewed. - Empty bins are kept. Equal width does not mean equal counts. A bin with no values still appears in
value_counts()with a count of 0. - Bin edges depend on the data. Running
pd.cut(x, bins=3)on two different datasets gives different edges, so the results are not comparable. If you need the same bins on new data, save the edges withretbins=Trueor define them explicitly. - Constant columns. If every value is the same, the range is 0 and the bins have no width. Check that the column varies before binning.
Summary
pd.cut(x, bins=n)makesnbins of equal width between the minimum and maximum.- Intervals are right-closed, and the lowest edge is moved down by 0.1% of the range so the minimum is included.
retbins=Truereturns the edges, andprecisiononly changes how they are displayed.- Equal width does not give equal counts, and outliers can leave most bins empty.
Exercise
For s = pd.Series([0, 50, 100]), what intervals does pd.cut(s, bins=4) create, and which interval does each value fall into?
Show answer
The range is 100, so each bin is 25 wide. The raw edges are 0, 25, 50, 75 and 100. The lowest edge is moved down by 0.1% of the range (0.1), so the intervals are (-0.1, 25], (25, 50], (50, 75] and (75, 100]. The value 0 falls in (-0.1, 25], 50 falls in (25, 50] because the right edge is closed, and 100 falls in (75, 100].