Exports from accounting and billing tools are rarely clean. A name has a trailing space. Someone typed a client in capitals. Amounts come out as text with commas. One invoice is in the file twice.

Total that as it is and you get the wrong answer, quietly. This walkthrough fixes a small made-up export (Lakeside Office Supply) with three pandas lines in Google Colab, then totals it. Free Colab is enough.

The messy export

Start a new notebook and run:

import pandas as pd 

In the next cell, type the data exactly as the export has it, mess included:

clients = ['maple dental ', 'Cobalt Legal', 'COBALT LEGAL', ' Harbor Accounting'] amounts = ['1,250', '860', '860', '640'] numbers = [101, 102, 102, 103] 

Look closely:

  • 'maple dental ' has a trailing space and no capitals
  • 'COBALT LEGAL' is the same client as 'Cobalt Legal'
  • ' Harbor Accounting' has a leading space
  • every amount is a string, and '1,250' has a comma
  • invoice 102 appears twice

Press Shift+Enter, then put it in one table:

invoices = pd.DataFrame({'client': clients, 'invoice': numbers, 'amount': amounts}) invoices 

Four rows. To a computer, "Cobalt Legal" and "COBALT LEGAL" are two different clients, and "1,250" is text, not a number.

What goes wrong if you total it now

Two things:

  1. A groupby('client') would put Cobalt Legal in two groups, so the same invoice is counted under two spellings.
  2. sum() on a text column doesn't add numbers. It joins the strings together ('860860'), or fails, depending on what's in the column.

Neither raises an obvious error in a quick look. That's why the cleanup comes first.

Clean it: two lines

invoices['client'] = invoices['client'].str.strip().str.title() invoices['amount'] = invoices['amount'].str.replace(',', '').astype(int) 

Line one:

  • .str.strip() removes spaces at both ends
  • .str.title() gives every name the same capitals: 'maple dental' becomes 'Maple Dental', 'COBALT LEGAL' becomes 'Cobalt Legal'

Line two:

  • .str.replace(',', '') removes the thousands comma
  • .astype(int) turns the text into whole numbers

The .str part matters. It applies the string method to every value in the column, not to the column object itself.

Drop the duplicate

Now both Cobalt Legal rows are spelled the same, so they're exact copies:

invoices = invoices.drop_duplicates() invoices 

Run it. Three clean rows:

client invoice amount
Maple Dental 101 1250
Cobalt Legal 102 860
Harbor Accounting 103 640

Why the order matters

drop_duplicates() only removes rows that match in every column. Before the cleanup, the two invoice-102 rows were 'Cobalt Legal' and 'COBALT LEGAL'. Different strings, so not duplicates, so the copy would have stayed in.

The rule is: clean the columns, then de-duplicate, then total. Swap the first two and the bug survives without any error message.

If you'd rather treat "same invoice number" as a duplicate no matter what the other columns say, you can tell pandas which column to compare:

invoices = invoices.drop_duplicates(subset=['invoice']) 

That's a judgment call. An invoice number repeated with a different amount is probably a correction, not a copy, and deserves a human look before you drop it.

Total it

invoices.groupby('client')['amount'].sum() 

Result:

client Cobalt Legal 860 Harbor Accounting 640 Maple Dental 1250 Name: amount, dtype: int64 

Three clients, three invoices, $2,750 in total. Without the cleanup, Cobalt Legal would have shown up twice and been double-counted.

Want it biggest first? Chain a sort:

invoices.groupby('client')['amount'].sum().sort_values(ascending=False) 

Reuse it on every export

Next month's export will have the same problems, just different rows. Keep the three cleaning lines together in one cell and run them every time:

invoices['client'] = invoices['client'].str.strip().str.title() invoices['amount'] = invoices['amount'].str.replace(',', '').astype(int) invoices = invoices.drop_duplicates() 

In real life you'd load the file with pd.read_csv(...) instead of typing the lists, and the same three lines apply.

A caveat on .title(): it capitalises every word, so names like "McDonald" or "ABC Plumbing" come out as "Mcdonald" and "Abc Plumbing". For grouping that's fine (they're consistent); for a customer-facing report you may want .str.upper() or a lookup table instead.

Checklist

  • [ ] import pandas as pd run first
  • [ ] Names cleaned with .str.strip().str.title()
  • [ ] Commas removed before .astype(int) (or it raises a ValueError)
  • [ ] Clean first, then drop_duplicates(), then groupby
  • [ ] Decide whether a repeated invoice number with a different amount is a copy or a correction
  • [ ] One total checked by hand against the source file

Practice it

This comes from a free step-by-step lesson where you type every line yourself in a practice copy of Google Colab that checks your work (10 steps, about 12 minutes): Python: Clean a Messy Invoice Export Before You Total It.