Python Coding · Cheat Sheet
Doing It With AI
Cheatsheet: Doing It With AI
One page for Topic 4. AI-generated pandas runs cleanly and fails logically in the same three places over and over: aggregation order, silent merge defaults, and NaN semantics. You don't write the fix — you run the checks that force it.
The AI-Python audit checklist
| # | Check | Question to ask |
|---|---|---|
| 1 | Row counts around every merge and filter | Did rows appear or vanish, and can each change be explained in business terms? (len(orders) 1,240 → len(merged) 1,113 = 127 orders and their revenue silently gone) |
| 2 | Any ratio being averaged? | A rate/CPA/percentage column through .mean() is guilty until explained — demand to know why it isn't sum-then-divide |
| 3 | Every fillna: what did NaN mean? | "Zero," "not measured," and "not applicable" are three different facts; filling with 0 is only right for the first — and .mean()/.sum() already skip NaN on their own |
| 4 | Merge defaults | No how= is an inner join nobody chose; ask for how='left' + validate='many_to_one' where the question says "all X" |
| 5 | One hand-checked example | Recompute one channel/campaign/region from raw data with a calculator; one slice fully traced validates the whole pipeline |
The three most common species
| Bug | Looks like | Reality |
|---|---|---|
| Averaged average | groupby('channel')['cvr'].mean() reports 6% | Blended rate is 2.1% — spiky channels distorted, rankings can invert. Fix: sum conversions, sum visits, divide once |
| Merge that ate the rows | orders.merge(managers, on='account_id') | Inner by default — unmanaged accounts and their revenue leave the report. The row count is the tell |
| fillna that manufactured data | fillna(0) on days-to-convert | NaN meant "never converted"; zeros make the worst campaigns look fastest. Count NaNs first: .isna().sum() |
The spec template for directing AI
Feed the AI five things — each converts a guess into a constraint:
| Provide | Example | Prevents |
|---|---|---|
Data shape: paste df.head() | Ten real rows — real column names, formats ("$1,200"), category values | Invented columns and formats |
Types: paste df.info() | Revenue is object, dates are strings, which columns have gaps | Missing cleaning steps |
| The grain | "One row per channel per day" | Mean-of-ratios, double-counting merges, grain-mismatched groupbys — if you say nothing else, say this |
| The question, with definitions and edge rules | "CPA = spend / conversions; Q2 = Apr 1–Jun 30; exclude test accounts; ties broken by earliest date" | Every undefined term is a decision delegated without noticing |
| Expected output shape + invariants | "One row per channel, columns: spend, conversions, CPA — 4 channels expected" | A checkable contract; six rows for four channels means something duplicated |
Ask for the plan before the code. "Describe every filter, merge, and calculation in plain English, in order." You can audit a plan with zero Python — wrong merge direction, wrong aggregation order, and invented definitions ("I'll define active as 30 days" — did you say that?) all surface for the price of one sentence.
Ask for assert tripwires — readable contract clauses that keep guarding every future rerun:
assert len(merged) == len(orders), f"Merge changed row count: {len(orders)} -> {len(merged)}"
assert df['revenue'].dtype == 'float64', "Revenue is not numeric — cleaning failed"
assert summary['channel'].nunique() == 4, "Expected 4 channels"
Standing instruction: "Add assert checks for row counts, dtypes, and output shape." Failing loudly is the good outcome.
Iterate surgically — name the line, the number, and the intended behavior:
- "Merged table has 1,113 rows but orders has 1,240 — which orders were dropped and why?"
- "You computed
.mean()on cvr — I want total conversions divided by total visits per channel." - "You filled NaN days-to-convert with 0, but NaN means never converted — exclude them instead."
Vague feedback ("still looks off") produces a random walk through plausible analyses.
The run-tiny-example verification habit
The cheapest, strongest control in the whole track: pick one slice and trace it by hand.
- Choose one channel, one customer, or one week.
- Ask: "Show me all raw rows for the Email channel and walk the CPA calculation by hand."
- Verify with a calculator against the reported figure.
Same habit at function level: trace one call with five concrete rows. Same habit at cleaning level: after any fix, check the condition ((df['revenue'] > 10000).sum() should be 0), not the printed "done."
The capstone audit workflow
For every analysis notebook that reaches your desk, in this order:
| Step | Action | What it catches |
|---|---|---|
| 1 | Counters, then Restart and Run All | Non-sequential brackets = stale outputs; compare every number before/after and confirm the conclusion's figures appear in the fresh final output |
| 2 | Row counts across every load, filter, merge | Each change needs a business explanation; divide counts by the grain (364 rows ÷ 91 days = 4 channels) to spot vanished categories |
| 3 | Hand-check one slice | Recompute one headline number from raw data; interrogate any .mean() on a ratio and any fillna while there |
| 4 | Conclusion vs corrected table | Does each claim cite a number that exists, current and verified? Would the recommendation survive the corrected numbers? |
In the worked capstone, this 15-minute procedure found three failures. A stale cell hid a $41→$58 CPA correction. An inner merge erased the best-performing channel (Referral, 4.0% cvr, zero tracked spend). And a mean-of-ratios inflated Social's cvr from 2.0% to 5.8%. The recommendation didn't just fail — it pointed backwards. Every bug ran without a single error.
When to escalate
Your audit proves the code computes what was asked; a domain expert catches when the question itself is wrong (wrong attribution window, wrong denominator convention). Add a human expert pass for anything moving real money or hard to walk back: budget reallocations, board materials, pricing, public statements. AI + your audit + one expert pass is a strong pipeline; AI + nobody is how confident wrong numbers reach boardrooms.
Traps
- Delegating the question along with the code — "tell me something interesting" outsources judgment, not labor.
- Accepting code without the plan — code-first puts your audit at the hardest layer; plan-first at the easiest.
- Treating the AI's self-review as verification — "I double-checked" from the model that wrote it is not a control; your row counts, hand-checked slice, and Restart and Run All are.
- Iterating by vibes — every round of feedback should name a line, a number, or a definition.
- Trusting a
fillnabecause it's labeled "cleaning" — the cleaning step can be the entire bug. - Skipping the counter check because the notebook looks tidy — display order is layout; the brackets are the history.
- Believing a plausible table — the capstone's numbers looked fine and every pillar of its conclusion was wrong.
Aligned to the official Python and pandas documentation.
Make this cheat sheet yours
Personalize this sheet — focus it however you study, or build one from the exact questions you keep getting wrong.