Chapter 8 — Segments and Seasonality

Breakdowns: Where the Truth Hides

16 min

The black box, “Metrics” compartment

The year is 2184. Vault-9, archive compartment 4. You're a data investigator standing in front of Kotomarket's “black box”: a marketplace for cat products, four delivery cities — Furrville, Catford, Meowfield, Tailford — and no living employees. The company is dead. The data remains.

QUERY, the archive's onboard AI, brings up the first : “Overall : 5.2%. A nice number. The board looked at it every morning. I checked. Repeatedly.”

L., log #12: “Overall conversion made us feel safe. One number, one green arrow. Then I broke it down and realized we were looking at the station's average temperature.”

A cadet lifts the edge of a large dashboard panel, revealing four beams of light of different brightness behind it; a robot cat sits by the cadet's leg.
The overall number feels reassuring until you break it down: the same data can look very different across segments, and the truth may be hiding in the breakdown.

Three terms to keep separate

A segment is a subset of rows defined by a value or condition on a feature: iOS visits, orders from Tailford, sign-ups from the referral channel.

A breakdown splits all the rows you're analyzing into non-overlapping segments based on one feature. A platform breakdown divides 8,000 visits into three groups with nothing left over. If the segment totals don't add up to the whole, the breakdown is incomplete: check your filters, missing values, and overlapping conditions.

A rate metric is a fraction: numerator / denominator. For example, conversion = purchases / visits; from here on, we'll call it CR, short for . Every rate comes with three numbers rather than one, but usually show only the third.

Here is the same weekly Kotomarket conversion rate, this time with all three numbers:

segmentvisits (denominator)purchases (numerator)CR
ios23971998.30%
android36091714.74%
web1994452.26%
total80004155.19%

The exact value is 415 / 8000 = 5.1875%: rounded to two decimal places, that's 5.19%; the dashboard rounds it to one decimal place, giving 5.2%.

Why overall conversion is a weighted average

Check the arithmetic by hand. Purchases add up: 199 + 171 + 45 = 415. Visits add up: 2397 + 3609 + 1994 = 8000. Conversion rates themselves don't add up, but you can reconstruct the overall rate from them like this:

CR_overall = 415 / 8000
           = 199/8000 + 171/8000 + 45/8000
           = (2397/8000) × (199/2397) + (3609/8000) × (171/3609) + (1994/8000) × (45/1994)
           = 0.300 × 8.30% + 0.451 × 4.74% + 0.249 × 2.26%
           = 2.49 pp    +  2.14 pp    +  0.56 pp
           = 5.19%

Each segment contributes to the overall number according to its weight — the segment's share of the total denominator. This is a weighted average.

Here's a tempting mistake that often comes up in interviews:

(8.30 + 4.74 + 2.26) / 3 = 5.10%     ← WRONG

It looks plausible, but it answers a different question: “What would conversion be if all three platforms were the same size?” Dividing by 3 silently assigns each platform a weight of 1/3, even though the actual weights are 0.300, 0.451, and 0.249. Here the error is only 0.09 pp, so it's easy to miss; in the lesson on Simpson's paradox, differences in weights will be large enough to reverse the conclusion.

Investigator's rule: whenever you see a rate metric, ask for three numbers — the numerator, the denominator, and the rate. The dashboard may show you one; you'll often need to retrieve the other two yourself.

In pandas, a breakdown is usually built with groupby: the same calculation is performed separately within each group. In this example, purchased is a purchase indicator containing 1 and 0, with no missing values. So count gives the number of visits, sum gives the number of purchases, and mean gives the conversion rate.

sessions.groupby('platform')['purchased'].agg(['count', 'sum', 'mean'])
# count — visits (denominator), sum — purchases (numerator), mean — CR

QUERY: “pandas is the bear of the archive: awkward-looking, but one swipe of its paw can tear open any box. groupby is its favorite claw.”

Segment contribution = share × conversiontraffic shareusersCRpurchasesiOS30%24008.3%199Android45%36004.7%170Web25%20002.3%468000 users total → 415 purchasesoverall CR = 415 / 8000 = 5.2%(8.3 + 4.7 + 2.3) / 3 = 5.1%this is WRONG — the segments differ in size
Bar length shows the segment's share of the denominator; the number on the right is the numerator. 199 + 171 + 45 = 415 purchases across 8,000 visits, giving 5.2%; averaging the three percentages gives a different number because it answers a different question. Segment sizes in the diagram are rounded to the nearest hundred.
The same conversion rate shown two ways: first as an aggregate, then as a breakdown with each segment's numerator, denominator, weight, and contribution.
python · pandas
Practice: write the code
QUERY suspects nobody ever analyzed Kotomarket by geography. Build a city breakdown — and include all three numbers.
  1. by_city — a table (DataFrame) with visits, buys, and cr columns for each city.
  2. worst_city and worst_cr — the city with the lowest conversion rate and its conversion rate.
  3. worst_visits — the number of visits in that city (don't draw conclusions without the denominator).
  4. sum_check — total purchases across cities divided by total visits: the breakdown must reconcile with the aggregate.
The sessions table (columns platform, city, purchased) has already been created.
python · pandas

A breakdown reveals patterns. It can also mislead you

After your first useful breakdown, you may be tempted to blame the worst-performing segment. Don't rush: there are at least four traps to watch for.

1. Low level ≠ drop. Web converts at 2.26% — that's its current level. Whether it dropped is a question about change, and one week of data isn't enough to answer it. Maybe Kotomarket's web conversion has always been around 2.2–2.4% because the site is old, there are no push notifications, and much of the traffic comes from random searches. In that case, 2.26% is normal rather than a new event, and the company should have “fixed web” three years ago rather than tonight. Keep these three questions about a segment separate:

questionwhat you look atwhat the answer means
what's the level?rate for the periodhow different the segment is from others
how did it change?this period's rate and the same segment's historywhether a new drop actually happened
how much does it weigh?the segment's share of the overall metric's denominatorhow much it can affect the overall result

When you're looking for the cause of an overall drop, start with segments that fell noticeably and carry a lot of weight. A low rate compared with other segments is useful additional evidence, but it doesn't show that the decline happened now. A segment that has always been low is usually a long-standing feature of the business rather than the cause of a new problem.

2. Small segments are noisy. Consider two segments that look similar at first:

  • 3 purchases out of 30 visits = 10.0%. With the same 30 visits, 4 purchases would give 13.3% (+3.3 pp), while 2 purchases would give 6.7% (−3.3 pp). A single purchase changes the metric by a third relative to its original value.
  • 350 purchases out of 10,000 = 3.50%. With the same 10,000 visits, 351 purchases would give 3.51%. To move that number by 3 pp, the purchase count would need to change by 300; a jump that large is very unlikely to happen by chance.

The 30-visit segment really does calculate to 10%, but the estimate is unstable: one additional purchase would change it substantially. We'll learn how to quantify that uncertainty in the statistics chapter. Until then, use a rough rule: if the denominator has a small count, don't base a conclusion on that segment. Collect more data or combine segments.

3. A breakdown can mix factors together. Tailford has the worst — but what if 80% of Tailford's visits come from web? Then what looks like a “city effect” may actually be the platform effect in disguise. A single breakdown doesn't separate causes; it tells you where to investigate. To examine two factors together, break the data down by both (groupby(['city', 'platform'])) and compare segments while holding the second feature fixed: comparing web traffic in Tailford with web traffic in Catford is fairer than comparing the two cities as a whole. This reduces confounding between those two factors, but it still doesn't establish causality.

4. If you inspect fifty breakdowns, you can easily find an “anomaly” in pure noise. City × platform × channel × app version × cat breed can produce hundreds of segments, and some will have noticeably higher or lower metrics purely by chance. A pattern discovered through a blind search is a hypothesis, not a conclusion; test it on another period or with new data. A breakdown chosen in advance to test a specific mechanism (“if logistics is broken, differences should appear across delivery cities”) provides stronger evidence.

L., log #13: “Tailford converts at half the rate of the others, and it did in all eight months I checked. The cats aren't the problem — logistics is: food takes nine days to get there, so customers leave for local stores. I wrote a report. The system says it was opened 0 times.”

QUERY, after a pause: “Zero is a metric too. The worst one I've ever seen. And I've seen a lot.”

Practice: write the code
The archive contains three segments, each with data for this week and its own history for the previous four weeks (table segments: visits, buys, hist_visits, hist_buys). For each segment, calculate the current cr, historical hist_cr, and change in percentage points as delta_pp ((cr - hist_cr) * 100). Then assign three diagnoses, using each segment once:
  • dropped — the segment that actually declined relative to its own history;
  • low_but_stable — the segment with a low rate but no change;
  • too_small — the segment with too few observations to support a conclusion.
The values should be the segment-name strings from the segment column.
python · pandas
Check yourself
Kotomarket's iOS conversion rate is 8.3%, Android is 4.7%, and Web is 2.3%. An analyst averages the three values, gets 5.1%, and reports it as the overall conversion rate. What's wrong?
Check yourself
A breakdown across 40 app versions shows version 3.7.1 with an 11% conversion rate versus the usual 5%. It has only 62 visits. What should the analyst do?
Key takeaways
  • A segment is a subset of rows; a breakdown should divide all rows into segments without losing any; a rate metric has three numbers: numerator, denominator, and rate.
  • An overall rate metric is a weighted average of segment metrics; the weights are the segments' shares of the denominator: 0.300 × 8.30% + 0.451 × 4.74% + 0.249 × 2.26% = 5.19%.
  • A simple average of percentages answers a different question — “what would the average metric be if all segments had equal weight?” — and gives a different result: 5.10%.
  • groupby(...).agg(['count', 'sum', 'mean']) gives you all three numbers at once. Segment numerators and denominators should add up to the overall — a quick check that no data was lost.
  • A metric's level, its change, and the segment's weight answer three different questions. The segment most likely to explain a major overall drop combines a large decline with a large weight.
  • Small segments are noisier: 3/30 is less stable than 350/10,000. A single breakdown can't separate possible causes, and checking dozens of segments can produce an “anomaly” even from random noise.

Next: how segment weight and contribution to the aggregate change turn a breakdown from an observation into an argument.