Imagine you sell prints through your own site and at local markets. You ask an AI assistant to write a Python script that totals order values by sales channel. Its report shows £45 for the website and £30 for markets. The figures look right until you compare them with the order export: the four orders were worth £100. One £25 order had no sales channel recorded. It existed in the source file and never appeared in the breakdown.
This is a useful test for any AI-written report that groups rows. A missing label does not make an order worthless or out of scope. If the report silently excludes the row, a reader may mistake incomplete attribution for lower sales. Decide how the report should account for unlabelled orders before you decide what the chart should look like.
See what pandas is grouping
The pandas reference for DataFrame.groupby documents a default of dropna=True. That default excludes rows with missing values in the grouping keys from the grouped result. Use dropna=False to retain missing keys as groups. This option was added in pandas 1.1.0 in July 2020. Here the grouping key is the channel, not the order amount. If the channel is missing and the amount is £25, it is the missing channel that determines whether the default grouping includes that row.
Take a small example where the channel is Website for two orders worth £40 and £5, Market for one order worth £30, and genuinely missing for a fourth order worth £25. With those amounts already stored as whole pence, orders.groupby('channel')['amount_pence'].sum() gives 4,500 and 3,000 pence. With orders.groupby('channel', dropna=False)['amount_pence'].sum(), the grouped result also accounts for the 2,500 pence with no channel. Neither call changes the four input orders.
Check how the import represents missing labels. An empty string, a space and a literal label such as ‘unknown’ may arrive as text rather than missing values. Decide which values count as unassigned for this export, and normalise only the ones covered by that rule. For instance, if ‘Unknown’ is already a category assigned by your team, do not automatically fold it into orders whose channel was never recorded. Those two groups may have different owners and different routes to a correction.
Keep uncertainty visible without inventing a channel
In a report that should cover every eligible order, show the missing group under a label such as ‘Channel not recorded’. Apply that wording for display and preserve the original value in the source. If someone later confirms that the £25 order came from a market, you can move it to the correct category without changing the overall order total. Avoid filling missing values with the most common channel simply to make the report look complete.
A different brief might ask for performance by confirmed channel only. In that case, £75 can be the correct subtotal, provided it is labelled as confirmed-channel orders and the £25 difference is explained alongside it. Be careful with percentages: the website’s £45 represents 60% of the £75 with known channels and 45% of the full £100. Both figures describe the same orders. A heading that says only ‘website share’ leaves the reader to guess which total was used.
If you add a second grouping key, such as region, write down what should happen when either field is missing. An order with a known channel but no region can still contribute to a channel-only report. It might also belong in a missing-region group in the detailed breakdown. Keep the explanation alongside the published report so that readers can distinguish a change in coverage from a change in sales.
Give the AI a test it cannot pass by accident
Ask for a synthetic fixture with the four orders above and specify the outputs before looking at the code. Run it through the same import, filtering and grouping steps as the real export. For this example, assume every amount is present and every order is eligible. That isolates the effect of the missing channel. Then check the output at two levels:
- Reconcile the amounts. Compare the sum before grouping with the sum of all reported groups after the same eligibility filters. Expect 10,000 pence for the fixture. If the breakdown deliberately omits unassigned orders, expect 7,500 pence in the breakdown and show the remaining 2,500 pence separately.
- Reconcile the rows. Check the grouped row counts as well as the amounts. A zero-value order can disappear without changing the money total. Positive and negative amounts can cancel each other out if the report includes adjustments. A row-count check gives you a separate signal.
- Inspect the exported report. Check that its chart, spreadsheet or PDF carries the unassigned line or its explanation. A correct intermediate table is little help if the final presentation drops the row again or changes the denominator.
If the channel comes from a lookup, check that step too. An earlier MaryChuks guide explains how missing IDs can match during a pandas merge. That is a separate failure: a lookup can assign the wrong channel before the grouping even starts. Keeping missing groups visible helps you see unresolved rows, while checking the lookup helps ensure that the labels you do have came from the right records.
Give the finished report a short coverage note: how many orders it includes, how many lack a channel, and which total any percentages use. The reader should be able to tell where the £25 went without opening a notebook or reading the script. That is a better acceptance test than whether the output simply looks tidy.
Sources
- pandas API reference: DataFrame.groupby.
- pandas 1.1.0 release notes (28 July 2020).
Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse
Subscribe to get the latest posts sent to your email.
2 thoughts on “AI-Written pandas: Don’t Let Unlabelled Orders Disappear From Totals”