pandas Group-By and Aggregation Pitfalls With Verification Steps
Avoid wrong totals when summarising.
The Open buttons send the prompt text as currently shown below to that site only when you click. Fill in the placeholders first.
[VERIFY] is an editorial flag, not a blank to fill. Wherever the prompt shows [VERIFY], check that item against a reliable source before you use it.
Help me summarise data correctly with groupby. Columns and a sample (invented rows): [COLUMNS_AND_SAMPLE]. The summary I want (by customer, by month, top N, share of total, running totals): [SUMMARY_NEEDED]. Explain and demonstrate, with code for each need: average of averages versus weighted average, missing values in grouping keys being dropped by default [VERIFY option names], categorical groupers producing empty groups, merging group results back, ranking within groups, and percentage of total within group. Worked traps with invented data: groups of 10 orders averaging 50 and 90 orders averaging 30 have an overall average of (10 x 50 + 90 x 30) / 100 = 32, not (50 + 30) / 2 = 40; a groupby on 1,000 rows that counts 940 because 60 rows have a missing key, silently excluded unless handled; and a share-of-total that is wrong because the denominator is taken after filtering. Verification for each: the sum of the group totals plus the excluded rows equals the raw total; print rows before and after each group operation and the sum of the main value column as an audit trail. Describe a test harness in words: a ten-row DataFrame with known groups and values, a hand-written expected table, and an equality check that tolerates floating-point rounding, with one case with a missing group key, one single-row group, and one duplicate row. Reconcile summaries to the raw total always. Treat version-dependent syntax as unconfirmed [VERIFY] and test on a tiny dataset first.
Fill in before use
Optional: type your details and the prompt above updates. Nothing is saved or sent.
Replace each bracketed placeholder with your own details, then paste into Any AI or another assistant.