Sub topic 4
Computing cumulative percentages
This chapter assumes you can tally events into categories and can use pandas Series, sort_values, and cumsum. Earlier chapters covered where the 80/20 rule comes from, how to count problems by category, and the split between the vital few and the useful many. Here you compute the numbers that become the line on a Pareto chart. The next chapter places that line on the chart.
What a cumulative percentage is
A cumulative percentage answers one question for each category in a ranked list: what share of the total is covered by this category and every category ranked above it?
The ordinary share of a category is its count divided by the grand total. The cumulative share is a running sum of those shares. It starts at the first category's share and rises with each row. Because every category is included once, it ends at exactly 100%.
The cumulative percentage is what lets you read "the first three causes account for 80% of defects" straight from the chart. Without it you would add bar heights by eye.
The formula
For categories ranked 1 to n with values v₁ ≥ v₂ ≥ … ≥ vₙ and total T = v₁ + … + vₙ:
- Share of category k: vₖ / T × 100
- Cumulative percentage at k: (v₁ + … + vₖ) / T × 100
The two properties below let you check any result at a glance.
- The cumulative percentage never decreases, because every value is zero or more.
- The last cumulative percentage is 100.
If either fails, something went wrong in sorting, filtering, or rounding.
A worked example by hand
Use the defect counts from one inspection period: Scratches 42, Misalignment 27, Wrong label 11, Cracks 8, Discoloration 5, and Other 7. The total is 100, so shares equal counts. A total of 100 hides the division step, so the next section also shows a case where it matters.
| Rank | Category | Count | Share % | Cumulative count | Cumulative % |
|---|---|---|---|---|---|
| 1 | Scratches | 42 | 42 | 42 | 42 |
| 2 | Misalignment | 27 | 27 | 69 | 69 |
| 3 | Wrong label | 11 | 11 | 80 | 80 |
| 4 | Cracks | 8 | 8 | 88 | 88 |
| 5 | Discoloration | 5 | 5 | 93 | 93 |
| 6 | Other | 7 | 7 | 100 | 100 |
Three categories reach exactly 80%. Real data rarely lands that neatly, and the later sections cover the cases where it doesn't.
The calculation as a pipeline
The order of the steps matters. Sorting must come before the running sum, and the division must use the grand total of all categories, including "Other".
Why the order of rows matters
A running sum depends entirely on row order. The same six counts give different cumulative columns depending on how you arrange them.
Suppose you sorted strictly by value, so "Other" (7) moved ahead of Discoloration (5):
| Row order | Cumulative % after row 5 |
|---|---|
| Other kept last | 93 (after Discoloration) |
| Strict sort by value | 95 (after Other) |
The tail of the curve changes, and "Other" now appears as a fifth cause you could act on. That is misleading, because "Other" is a mix of unrelated small causes. The convention is therefore to rank the named categories by value and always place "Other" last, even when it is larger than some named categories. If "Other" is larger than your biggest named category, treat that as a sign that your categories are too coarse and split it.
For ties, any order among equal values is mathematically valid. Use a stable sort so the order is reproducible between runs.
Computing it in pandas
The first listing keeps the logic in one reusable function. It validates its input, sorts with "Other" last, and returns the share and the cumulative percentage.
import pandas as pd
def pareto_table(values, last="Other"):
"""Return counts, shares, and cumulative percentages, ranked for a Pareto chart."""
s = pd.Series(values)
if s.empty:
raise ValueError("no categories given")
if (s < 0).any():
raise ValueError("negative values cannot be ranked")
total = s.sum()
if total == 0:
raise ValueError("total is zero, so shares are undefined")
if last in s.index:
main = s.drop(last).sort_values(ascending=False, kind="stable")
s = pd.concat([main, s[[last]]])
else:
s = s.sort_values(ascending=False, kind="stable")
return pd.DataFrame({
"value": s,
"share_pct": (s / total * 100).round(1),
"cum_pct": (s.cumsum() / total * 100).round(1),
})
defects = {
"Scratches": 42,
"Misalignment": 27,
"Wrong label": 11,
"Cracks": 8,
"Discoloration": 5,
"Other": 7,
}
print(pareto_table(defects))The output matches the hand calculation:
value share_pct cum_pct
Scratches 42 42.0 42.0
Misalignment 27 27.0 69.0
Wrong label 11 11.0 80.0
Cracks 8 8.0 88.0
Discoloration 5 5.0 93.0
Other 7 7.0 100.0The function raises an error for three inputs that would otherwise give silent nonsense. An empty input has no ranking. A negative value makes the running sum fall, which breaks the "never decreases" property. A total of zero makes the division undefined, and pandas would return NaN or inf without complaint.
Round last, not first
The most common numerical mistake is to round the shares and then sum the rounded values. Rounding errors accumulate along the running sum.
Take three equal categories of 1 each. Each share is 33.333…%.
import pandas as pd
counts = pd.Series({"A": 1, "B": 1, "C": 1})
total = counts.sum()
wrong = (counts / total * 100).round(1).cumsum()
right = (counts.cumsum() / total * 100).round(1)
print(pd.DataFrame({"wrong": wrong, "right": right}))The wrong column ends at 99.9 and the right column ends at 100.0. The right version rounds only once, at the end, on the exact running sum.
wrong right
A 33.3 33.3
B 66.6 66.7
C 99.9 100.0Weighted measures: cost instead of count
Nothing in the method requires the values to be counts. If you rank categories by cost, downtime minutes, or scrap weight, the same pipeline applies: sort the totals per category, take the running sum, and divide by the grand total. Two points need care.
- Aggregate before ranking. If your raw data has one row per event, group by category and sum first, for example
df.groupby("category")["cost"].sum(). Ranking individual events is not the same as ranking categories. - Costs can be non-integers. The running sum then uses floating-point arithmetic, which is the topic of the next section.
The choice between count and cost changes the ranking itself and is covered in a later chapter. The cumulative calculation is identical either way.
Floating-point edges and the 80% threshold
With integer counts, the final cumulative value is exactly total / total, which is exactly 1. With float costs, a sum of many values can be off by a tiny amount, so a running sum might end at 99.99999999999999 instead of 100. Two habits protect you.
- Compare with a tolerance. Use
numpy.isclose(cum_pct.iloc[-1], 100)in a check, not== 100. - Do threshold tests without dividing, when you can. To ask whether a cumulative value is below 80% of the total, compare
cum * 100 < 80 * total. With integer counts, both sides are exact integers, so a category that lands exactly on 80% is classified correctly every time.
The following listing selects the vital few using the integer comparison. It keeps every category whose preceding cumulative value is still below 80%, so the category that crosses the line is included.
import pandas as pd
data = pd.Series({
"Scratches": 42,
"Misalignment": 27,
"Wrong label": 11,
"Cracks": 8,
"Discoloration": 5,
"Other": 7,
})
total = data.sum()
cum = data.cumsum()
preceding = cum.shift(fill_value=0)
vital_few = data[preceding * 100 < 80 * total]
print(vital_few)
print(f"{len(vital_few)} of {len(data)} categories give {vital_few.sum() / total:.0%} of defects")The shift step is the subtle part. A row's own cumulative value includes the row itself, so testing cum < 80 would stop one category too early whenever a category crosses the line. Testing the preceding value asks the right question: was the 80% mark still ahead of us when this category began?
Here "Wrong label" has a preceding cumulative of 69, which is below 80, so it is included. "Cracks" has a preceding value of 80, which is not below 80, so it is excluded. The result is 3 of 6 categories covering 80% of defects.
Edge cases to check before charting
| Situation | What happens | What to do |
|---|---|---|
| A category with zero count | Its cumulative value repeats the previous row | Drop it, or leave it out of the chart |
| One category dominates (for example 95%) | The curve starts near the top and flattens at once | Report it, then drill down into that category |
| Near-uniform counts | The curve is almost a straight diagonal | Conclude that no vital few exists. Do not force an 80/20 story |
| "Other" larger than the top named category | The tail hides real causes | Split "Other" into named categories |
| Missing values (`NaN`) | `sum` skips them but the row remains in the table | Decide explicitly whether to drop or fill them before computing |
| Mixed units in one column | Totals are meaningless | Convert to one unit before aggregating |
The near-uniform case is worth remembering. A cumulative percentage always rises to 100, so a table always looks like a ranking. Whether the ranking is useful depends on how steep the early part of the curve is.
Quick self-check list
Run these checks on any cumulative column before you chart it.
- The first cumulative value equals the first share.
- The values never decrease from row to row.
- The last value equals 100, within a tolerance for float data.
- The "Other" row, if present, is last.
- The total used in the denominator includes every category, including "Other".
Exercise
A support team logged 180 complaints: Late delivery 72, Damaged item 48, Wrong item 30, Billing 18, Other 12. Compute the cumulative percentage for each row, keeping "Other" last. Which category is the one that crosses the 80% line, and how many categories belong to the vital few under the "include the crossing category" rule?
Show answer
The total is 180. Cumulative counts are 72, 120, 150, 168, 180, which give cumulative percentages of 40.0, 66.7, 83.3, 93.3, and 100.0. The preceding cumulative percentage is below 80 for Late delivery (0), Damaged item (40.0), and Wrong item (66.7). It is 83.3 for Billing, which is not below 80. "Wrong item" is the category that crosses the 80% line (66.7 before it, 83.3 after it). The vital few are the first three categories, Late delivery, Damaged item, and Wrong item.
Exercise
A colleague computes shares as (counts / counts.sum() * 100).round(0) and then takes cumsum() of the rounded shares. For counts of 1, 1, 1, 1, 1, 1, 1 (seven equal categories), what does the last cumulative value come out as, and what is the correct fix?
Show answer
Each share is 14.2857…%, which rounds to 14, so the cumulative sum of rounded shares ends at 7 × 14 = 98 instead of 100. The fix is to take the running sum of the exact counts, divide by the total, and round only the final column: (counts.cumsum() / counts.sum() * 100).round(0). That column ends at exactly 100.
Exercise
Why is the test cum_pct < 80 a poor way to select the vital few, and what does the code in this chapter use instead?
Show answer
A row's cumulative value already includes the row itself, so cum_pct < 80 drops the category that crosses the 80% line, and the selection stops one category too early. The chapter's code tests the preceding cumulative value, cum.shift(fill_value=0) * 100 < 80 * total. It keeps the crossing category and uses integer arithmetic, so a category that lands exactly on 80% is classified correctly.