Skip to content
Start free

Reconcile a spending table that does not add up

An AI's spending by department misses $450. Reconcile it against the file and find each row that did not arrive, and why.

2 parts · 6 and 3 min

Free course. Your Maestro, an AI, .

Finance asked for a breakdown of this month's spending by department from an eight-row expense table (expenses). The AI ran two lines of and returned two numbers: Ops at $650 and Sales at $500.

If you check Ops ($400 + $250 = $650) and Sales ($300 + $200 = $500) against the approved claims for those two teams, both sums are right. That is why incomplete tables slip into reports: when each category's number checks out, the table looks finished.

Before you verify individual groups, run a between the AI's table and the source file. Summing the amount column across all eight rows in expenses gives $1,600. Summing the AI's department table gives $1,150 ($650 + $500). Somewhere between the raw file and the summary table, $450 disappeared.

Two ways rows vanish without an error

Look at the two lines the AI ran in the script below: first approved = expenses[expenses["status"] == "approved"], and then by_dept = approved.groupby("department")["amount"].sum(). Each line removes rows silently, for a different reason:

  1. An unannounced filter on line 1. The request asked for spending by department this month, without specifying whether pending or rejected claims belong in the total. The AI kept only rows where status == "approved" and never mentioned that rule in its output. Every row with rejected or pending in status is gone before the grouping starts.
  2. A blank grouping key on line 2. On the previous page, you saw how .count(), .sum() and .mean() ignore blank cells (None or NaN). In pandas, groupby("department") does the same thing to the grouping column: by default (dropna=True), it skips any row where department is blank instead of creating an unassigned group. When an approved claim has None in department, its dollar amount never enters by_dept. If you are unsure why a blank in the grouping column drops that row's amount too, ask your Maestro.

Try it

Run the AI's calculation and the reconciliation in the editor below. Then add two checks that account for the $450 gap step by step: first print the rows removed by the status filter (expenses[expenses["status"] != "approved"]), and then print the rows inside approved that have a blank department (approved[approved["department"].isna()]).

Your turn

Run the AI's code and the reconciliation. Then print the rows the filter removed and the approved rows with a blank department. You're done when those rows add up to the gap.

A good place to stop. Up next: Accounting for every missing dollar · 3 min

Accounting for every missing dollar

Tracing the rows in the same order as the code splits the $450 gap into two exact pieces:

  • Removed by the status filter ($300): E5 (Ops, rejected, $100), E7 (blank department, pending, $80) and E8 (Sales, pending, $120) were filtered out when expenses became approved. Five approved rows totaling $1,300 moved on to the next line.
  • Dropped by the blank grouping key ($150): E3 (approved, $150) survived the status filter, but its department is None, so approved.groupby("department") skipped it.

Notice why order matters when you classify E7 ($80). That row has both status == "pending" and a blank department. Because the status filter runs on line 1, E7 leaves the calculation before groupby runs on line 2. If you checked expenses["department"].isna() on the raw table instead of on approved, you would pull both E3 ($150) and E7 ($80) and count E7 twice in your reconciliation ($300 + $230 = $530, overshooting the $450 gap).

State the exclusions or surface the row

Dropping rows is not automatically a mistake. Leaving out rejected ($100) and pending ($200) claims is often the right accounting call when Finance asks for actual spending; the problem was hiding that filter from the reader. Dropping E3 ($150), on the other hand, omits an approved expense simply because its department cell was left blank.

You can keep E3 visible in the table by passing dropna=False to approved.groupby("department", dropna=False)["amount"].sum() or by filling the missing label with approved["department"].fillna("Unassigned") before grouping. And whenever a summary table leaves out rows from the source file, attach a caption that reconciles the two totals:

Approved spending by assigned department ($1,150). Excludes $150 in approved claims with no department assigned (E3) and $300 in pending or rejected claims (E5, E7, E8); total across all 8 claims in the file is $1,600.

If you want feedback on your code or on how you wrote the reconciliation note, share it with your Maestro.

Check yourself: Suppose you keep the status filter (approved = expenses[expenses["status"] == "approved"]) and change the second line to approved.groupby("department", dropna=False)["amount"].sum(). What will the new table total, and which rows from expenses are still left out?