Chapter 3 Key Takeaways — Python and pandas for Cricket
Key Takeaways
- Python is a calculator with a memory. Variables hold one value (
runs = 43), lists hold a sequence (an over:[0, 0, 6, 4, 6, 6]), dictionaries hold labelled values (a scorecard line), and functions package a calculation once (batting_strike_rate(runs, balls)). - A DataFrame is a list of scorecard lines with superpowers. One row per delivery, one column per fact. The IPL table in the book's data has 295,732 rows and 38 columns; a column on its own is a Series.
- The workflow is: load → filter → group → merge/reshape → check → plot. Almost every table in this book is a boolean filter followed by a
groupby, sometimes amergewith the matches table, sometimes apivot_table. - Vectorise; don't loop over rows.
groupbycomputed every IPL batter's strike rate about 109 times faster than aniterrowsloop on the chapter's run. Loops are for small, genuinely sequential jobs — like inferring a batting order. - Definitions are decisions. Balls faced exclude wides; super overs are excluded; runs conceded exclude byes and leg-byes; a wicket ball can be a "dot" or a "wicket" depending on your table. Use the toolkit's summaries and state any definition you change.
- Every rate needs its sample beside it. A strike rate of 290 off 10 balls and one of 157.7 off 265 balls are not the same kind of fact.
- Real data is untidy in specific ways. Seasons are text and sometimes two years (
'2007/08','2020/21','2025/26'); team names change; "missing" usually means "not applicable"; no-result innings distort averages. - The data records outcomes, not causes. You rebuilt a full scorecard from 252 deliveries, but not where Krunal Pandya bowled or how Miller was caught. Data tells you where to point your eyes.
Metrics & Formulas
| Metric | Formula (book definition) | Real example from this chapter |
|---|---|---|
| Strike rate (batting) | 100 × runs off the bat ÷ balls faced (wides excluded) | Kohli, IPL 2025 final: 100 × 43/35 = 122.86 |
| Economy rate | 6 × runs conceded ÷ legal balls | Krunal Pandya, IPL 2025 final: 6 × 17/24 = 4.25 |
| Batting average | runs ÷ outs (NaN if never out) | Kohli, IPL 2026: 675/12 = 56.25 |
| Dot % (share of legal balls) | (runs == 0).mean() × 100 |
IPL 2026 powerplay: 41.4% |
| Boundary ball % | (4s + 6s) ÷ balls faced × 100 | Suryavanshi, IPL 2026: 41.28% |
| Run rate (team/phase) | 6 × all runs ÷ legal balls | IPL 2026 powerplay: 10.10 |
| Required run rate | 6 × runs needed ÷ balls left | SA after 15 overs, T20 WC final 2024: 6 × 30/30 = 6.00 |
| Average first-innings total | mean of innings-1 totals, matches with a winner, no D/L | IPL: 157.5 (2012) → 195.7 (2026) |
Common Mistakes (and the fix)
| Mistake | What happens | Fix |
|---|---|---|
balls["season"] == 2026 |
zero rows, no error | compare with text ("2026") or use start_date.dt.year |
| Counting wides as balls faced | Kohli's career SR 131.07 instead of 134.80 | filter wides == 0 before counting, or use batting_summary |
| Including super overs | numbers that don't match anyone else's (Kohli 134.86) | ~balls["super_over"] |
and / or between Series |
ValueError: truth value … ambiguous |
& / \| / ~ |
| No brackets around conditions | TypeError: Cannot perform 'rand_'… |
(cond1) & (cond2) |
Using runs_total for batters |
extras credited to the batter | runs_batter for batting, runs_conceded() for bowling |
| Leaderboard without a minimum | an 11-ball cameo tops the strike-rate table | bs[bs["balls"] >= 150], and show the balls column |
| Averaging over no-result innings | Pakistan's 2025 WC "average total" 89.9 | filter matches with a winner (and no D/L if needed) |
| Assuming team names are stable | "Royal Challengers Bengaluru" has three seasons, not nineteen | check the name list; map old names explicitly |
| Assigning to a filtered slice | SettingWithCopyWarning (pandas 2) |
.copy() after filtering |
Decision Framework: Before You Trust a Table
- Which rows? State the competition, seasons (as text or year), phase, and whether super overs, wides or no-results are in or out.
- Which definition? Runs off the bat or total? Legal balls or all deliveries? Wicket balls as dots or not?
- How many? Print the row count after every filter and merge. Zero rows is a bug until proven otherwise.
- Enough sample? Put the balls (or matches) beside every rate. Set a minimum and say what it is.
- Does it reconcile? Bowlers' runs + byes + leg-byes = team total; hand-typed values = data; your
groupby= the toolkit's summary. - Is it a fact or an artefact? Names, labels and missing values can manufacture a "finding". Check before you publish.
- What can't it see? Line, length, fields, drops, intent. Say so.