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 Glossary word, you need to answer two questions: which column identifies one invoice, and are the repeated rows actually identical copies?
Start with the table's Glossary word. In this export, each row is supposed to represent one invoice, identified by invoice_id:
I-01(Acme, $500) andI-03(Acme, $200) are two different invoices for Acme.I-02(Birch, $300) appears twice, andI-04(Cole, $450) appears twice.I-05(Birch, $300) is a separate invoice for Birch that happens to have the same $300 amount asI-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:
- Find repeated IDs.
invoices["invoice_id"].duplicated(keep=False)marks every row whoseinvoice_idappears more than once, including the first copy, so you can inspect the repeated rows side by side. If you are unsure whatkeep=Falsechanges compared withduplicated(), ask your Maestro to show both on this table. - 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 whetherI-02andI-04are exact duplicates from the export glitch or two different records that accidentally received the same ID. - Deduplicate on the identifier and measure the inflation.
invoices.drop_duplicates(subset="invoice_id")keeps the first row for eachinvoice_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
clientandamount. Runninginvoices.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-02andI-05), this removesI-05along with the repeat ofI-02and brings the total down to $1,450. - Relying on bare
drop_duplicates()without naming a column. Callinginvoices.drop_duplicates()with nosubsetonly removes rows that match across every column. It gives $1,750 here only because the repeatedI-02andI-04rows are identical in all three columns. If the table also had an export timestamp column that differed between copies, baredrop_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 output should show:
- The AI's raw total across all 7 rows is $2,500.
- Two invoice IDs repeat:
['I-02', 'I-04'], andinvoices.duplicated().sum()returns2, confirming that both repeats are identical copies of earlier rows (I-02for Birch at $300 andI-04for Cole at $450). - Deduplicating with
subset="invoice_id"gives the true total of $1,750 across the 5 distinct invoices (I-01throughI-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 dropsI-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.