The Bank Says One Number and the Ledger Says Another: Reconciling Transactions with Python and Pandas
The Bank Says One Number and the Ledger Says Another: Reconciling Transactions with Python and Pandas
Month-end close was tomorrow morning. The accountant had two exports open and a nervous look on her face. "The bank says one thing and our books say another. Can you help me find out why?"
There was just one problem. The two files didn't look anything alike.
The ledger had dollar signs and the bank did not. The bank also showed every payment as a negative number. Dates came in three different formats. The bank described every payment in cryptic capital letters. There was a blank row in the ledger. And the bank posted some payments days after we recorded them.
She could have ticked through every line by hand. But there would be another month-end next month, and the one after that. A small Python pipeline can do the ticking for her every time.
The idea was simple:
Bank export + ledger export → Python + pandas → Reconciliation exceptions list
The program is just the pipe. What matters is what happens to the data as it moves through it.
The Data Going In
Here is the ledger export:
entry_id,description,amount,entry_date
L001,Acme Supply,"$2,400.00",03/02/2026
L002,Brightline Freight,"$865.50",2026-03-03
L003,Payroll Run,"$12,000.00",March 5 2026
L004,Office Rent,"$3,200.00",03/05/2026
L005,Northwind Office,"$129.99",03/10/2026
L006,Janitor Supplies,$75.00,03/12/2026
,,,
L007,Delta Cleaning,"$1,150.00",03/15/2026
L008,Janitor Supplies,$75.00,03/26/2026
L009,Zenith Printing,$480.00,03/28/2026
And here is the bank export:
txn_id,memo,amount,post_date
B501,ACH ACME SUPPLY,"-2,400.00",03/04/2026
B502,CHK BRIGHTLINE,-865.50,2026-03-05
B503,PAYROLL,"-12,000.00",03/05/2026
B504,RENT,"-3,200.00",03/05/2026
B505,NORTHWIND OFFICE,-129.99,03/10/2026
B506,JANITOR SUPPLIES,-75.00,03/13/2026
B507,DELTA CLEANING,"-1,150.00",03/25/2026
B508,JANITOR SUPPLIES,-75.00,March 27 2026
B509,BANK SERVICE FEE,-35.00,03/31/2026
Look at the descriptions.
Acme Supply
in the ledger is
ACH ACME SUPPLY
at the bank. Matching on names would be a headache. The amounts, though, are
the same in both files, so that's what we'll match on.
Two details need care. First, the bank posts payments a few days late, so the
dates won't match exactly. We'll allow three days, set up as a named setting
that's easy to change. Second,
$75.00
appears twice in each file. If we aren't careful, one payment could match
both.
One caution before we start. Matching on amount alone is a simplification. Two different vendors with the same amount could pair up wrongly. That's one more reason a person reviews the list at the end.
The Program
The pipeline cleans both files the same way, lines them up on amount, and labels every row.
import pandas as pd
# Setting: How many days apart a bank date and ledger date can be.
GRACE_DAYS = 3
# Input: Read both exports as text.
ledger = pd.read_csv("ledger_export.csv", dtype=str).dropna(how="all")
bank = pd.read_csv("bank_export.csv", dtype=str).dropna(how="all")
# Process: Turn amounts into whole cents and clean the dates, the same way in both files.
for df, date_col in [(ledger, "entry_date"), (bank, "post_date")]:
dollars = df.pop("amount").str.replace(r"[$,]", "", regex=True).astype(float)
df["amount_cents"] = (dollars * 100).round().abs().astype(int)
df[date_col] = pd.to_datetime(df[date_col], format="mixed")
# Process: Number repeated amounts so identical payments pair up one to one.
ledger["nth"] = ledger.sort_values("entry_date").groupby("amount_cents").cumcount()
bank["nth"] = bank.sort_values("post_date").groupby("amount_cents").cumcount()
# Process: Line up the two files on amount.
merged = ledger.merge(bank, on=["amount_cents", "nth"], how="outer", indicator=True)
merged["days_apart"] = (merged["post_date"] - merged["entry_date"]).dt.days.abs()
# Process: Label every row.
merged["status"] = "Matched"
merged.loc[merged["_merge"] == "left_only", "status"] = "Only in ledger"
merged.loc[merged["_merge"] == "right_only", "status"] = "Only in bank"
merged.loc[merged["days_apart"] > GRACE_DAYS, "status"] = "Timing gap"
# Keep only the rows a person needs to look at.
exceptions = merged[merged["status"] != "Matched"].copy()
exceptions["amount"] = exceptions["amount_cents"] / 100
for col in ["entry_date", "post_date"]:
exceptions[col] = exceptions[col].dt.strftime("%Y-%m-%d")
exceptions["days_apart"] = exceptions["days_apart"].astype("Int64")
# Output: Write the exceptions to a new CSV file.
columns = ["status", "amount", "entry_id", "description", "entry_date",
"txn_id", "memo", "post_date", "days_apart"]
exceptions[columns].to_csv("reconciliation_exceptions.csv", index=False, float_format="%.2f")
Read it from top to bottom and the data flow is easy to follow.
Both files come in as text. Amounts become whole cents, so $129.99 turns into
12999. Whole numbers compare more reliably than decimals. The
abs()
turns the bank's negatives into positives, so both files line up. Dates get
cleaned the same way. Then comes the
nth
trick. It numbers each repeated amount, so the first $75.00 in the ledger
pairs with the first $75.00 at the bank, and the second with the second.
The outer merge keeps everything, including rows with no partner on the other
side. Each row then gets a label. If a payment only appears in one file, it
says so. If the dates are more than
GRACE_DAYS
apart, it's a timing gap. Everything else is a clean match, and we drop those
from the output. Nobody needs to review the rows that already agree.
The Data Coming Out
The new
reconciliation_exceptions.csv
looks like this:
status,amount,entry_id,description,entry_date,txn_id,memo,post_date,days_apart
Only in bank,35.00,,,,B509,BANK SERVICE FEE,2026-03-31,
Only in ledger,480.00,L009,Zenith Printing,2026-03-28,,,,
Timing gap,1150.00,L007,Delta Cleaning,2026-03-15,B507,DELTA CLEANING,2026-03-25,10
Nine ledger entries and nine bank lines became three things to look at. Seven pairs matched cleanly and aren't listed. That's the point.
That's our real result. We turned two files that disagreed into a short list somebody can act on:
bank_export.csv
+
ledger_export.csv
→ Python + pandas →
reconciliation_exceptions.csv
Now It's the Accountant's Data
At this point, our Python program has done its job.
The accountant can open the exceptions file in Excel and work through three lines. The $35 bank fee isn't in the books, so she records a journal entry. The $480 check to Zenith Printing hasn't cleared, so she leaves it as an outstanding item or nudges the vendor. The Delta Cleaning payment cleared ten days late, which is probably fine, but she may want to confirm.
She can then sign off on the close with a clear paper trail.
Notice what the program didn't do. It didn't decide what the fee was for, and it didn't judge whether the late payment was a problem. Those calls belong to a person who knows the business.
The code is the pipe.
The data, and where it's going, is the story.
