Skip to content
Start free

An invoice total counted twice

An export repeated some invoices. Find the true repeats, measure how much they inflate the total, and avoid removing a real invoice.

2 parts · 3 and 6 min

Free course. Your Maestro, an AI, .

When an export glitches, it can write the same record into a file more than once. An AI asked to total this month's invoices ran invoices["amount"].sum() on a 7-row export and reported $2,500. Because sum() adds every row in the table, any repeated row inflates the total just as quietly as a dropped row shrinks it.

Removing duplicates takes care because an overly broad fix can delete real data. Before you drop any rows in , you need to answer two questions: which column identifies one invoice, and are the repeated rows actually identical copies?

Start with the table's . In this export, each row is supposed to represent one invoice, identified by invoice_id:

  • I-01 (Acme, $500) and I-03 (Acme, $200) are two different invoices for Acme.
  • I-02 (Birch, $300) appears twice, and I-04 (Cole, $450) appears twice.
  • I-05 (Birch, $300) is a separate invoice for Birch that happens to have the same $300 amount as I-02.

A client can receive more than one invoice in a month, and two real invoices can share the same dollar amount. That means invoice_id defines an invoice, while client and amount do not.

Before dropping anything, check which IDs repeat and whether the whole rows match:

  1. Find repeated IDs. invoices["invoice_id"].duplicated(keep=False) marks every row whose invoice_id appears more than once, including the first copy, so you can inspect the repeated rows side by side. If you are unsure what keep=False changes compared with duplicated(), ask your Maestro to show both on this table.
  2. Confirm the rows are identical copies. invoices.duplicated().sum() checks every column at once and counts how many rows are exact repeats of an earlier row. Checking both tells you whether I-02 and I-04 are exact duplicates from the export glitch or two different records that accidentally received the same ID.
  3. Deduplicate on the identifier and measure the inflation. invoices.drop_duplicates(subset="invoice_id") keeps the first row for each invoice_id. Subtracting that cleaned sum from the AI's $2,500 total measures how much the repeats added.

Watch out for two traps when cleaning repeats:

  • Deduplicating on client and amount. Running invoices.drop_duplicates(subset=["client", "amount"]) treats any second invoice for the same client and dollar amount as a repeat. Because Birch has two real $300 invoices (I-02 and I-05), this removes I-05 along with the repeat of I-02 and brings the total down to $1,450.
  • Relying on bare drop_duplicates() without naming a column. Calling invoices.drop_duplicates() with no subset only removes rows that match across every column. It gives $1,750 here only because the repeated I-02 and I-04 rows are identical in all three columns. If the table also had an export timestamp column that differed between copies, bare drop_duplicates() would miss the repeat.

Try it

Run the exercise below to find the repeated invoice_id values, confirm how many rows are identical copies, compute the true total and the inflation, and compare that result against deduplicating on client and amount.

A good place to stop. Up next: the exercise · 6 min

Your turn

Run the AI's total. Find invoice_id values that repeat and check the repeated rows are identical. Compute the total without the repeats, and compare with deduplicating on client and amount. You're done when you can say which total is right and why.

Your output should show:

  • The AI's raw total across all 7 rows is $2,500.
  • Two invoice IDs repeat: ['I-02', 'I-04'], and invoices.duplicated().sum() returns 2, confirming that both repeats are identical copies of earlier rows (I-02 for Birch at $300 and I-04 for Cole at $450).
  • Deduplicating with subset="invoice_id" gives the true total of $1,750 across the 5 distinct invoices (I-01 through I-05). The two repeated rows inflated the AI's total by $750 ($300 + $450).
  • Deduplicating with subset=["client", "amount"] gives $1,450, which is $300 too low because it also drops I-05 (Birch's second real $300 invoice). If your numbers do not match these, share your code with your Maestro to trace which rows were dropped.

Check yourself: In this 7-row export (raw total $2,500), an analyst runs invoices.drop_duplicates(subset=["client", "amount"])["amount"].sum() and gets $1,450 instead of $1,750. Why did that calculation lose $300?