A warehouse wants to count packages in 500-gram bands. A package weighing 499 grams belongs below the first cutoff, and a package weighing exactly 500 grams goes in the next band. Decide where those cutoff values belong before you count anything.
Use pandas.cut() with explicit edges and right=False to include each lower endpoint and exclude each upper endpoint. Keep a separate record of missing weights and measurements outside your bands so you can account for all the packages.

Set the weight bands
This fictional inventory has one row per package. All recorded weights are whole grams. The negative entry is included to test the range check, and P07 has no recorded weight.
| Package | Weight (g) |
|---|---|
| P01 | 499 |
| P02 | 500 |
| P03 | 999 |
| P04 | 1000 |
| P05 | 2000 |
| P06 | -5 |
| P07 | Missing |
| P08 | 0 |
The edges are 0, 500, 1000, 1500, and 2000 grams. Five edges make four bands. The first band contains weights from 0 up to, but excluding, 500 grams. The next band starts at 500 grams, so no two bands can claim that measurement.
Assign a band and count the packages
Run this example in Python with pandas installed. The nullable integer type Int64 keeps the whole-gram measurements and allows the missing weight.
import pandas as pd
packages = pd.DataFrame({
"package": ["P01", "P02", "P03", "P04", "P05", "P06", "P07", "P08"],
"grams": pd.Series([499, 500, 999, 1000, 2000, -5, None, 0], dtype="Int64"),
})
edges = [0, 500, 1000, 1500, 2000]
labels = ["0 to <500", "500 to <1000", "1000 to <1500", "1500 to <2000"]
packages["band"] = pd.cut(packages["grams"], bins=edges, labels=labels, right=False)
missing = packages["grams"].isna()
outside = packages["grams"].notna() & packages["band"].isna()
counts = packages["band"].value_counts(sort=False)
print(counts.to_string())
print("Missing weight:", ", ".join(packages.loc[missing, "package"]))
print("Outside the bands:", ", ".join(packages.loc[outside, "package"]))
assert int(counts.sum() + missing.sum() + outside.sum()) == len(packages)
Expected output:
band
0 to <500 2
500 to <1000 2
1000 to <1500 1
1500 to <2000 0
Missing weight: P07
Outside the bands: P05, P06
P08’s recorded zero falls in the first band. P02’s 500 starts the second band, and P04’s 1000 starts the third. The 1500-to-under-2000 band stays in the output with a count of zero because pandas.cut() produced a categorical column. sort=False leaves the counts in band order.
Check why a package has no band
Both an unrecorded weight and a recorded weight outside the edges leave the band missing. The missing mask checks the input weight. The outside mask checks for a recorded input with no assigned band. Here, 2000 is the excluded final edge, and -5 falls below the first edge.
Five packages have a band, one is missing a weight, and two are outside the range. The final assertion checks that these counts account for all eight rows. Before using your own report, confirm the boundary rule with whoever defines the bands and keep the exception lists available for review. See MathBlog’s guide to finding and handling missing values in pandas if the input weights themselves need cleaning.
The example was run with Python 3.11.3 and pandas 2.3.3.