Python for Excel Users: Read, Clean and Summarise a Spreadsheet with pandas

Python for Excel Users: Read, Clean and Summarise a Spreadsheet with pandas

πŸ“Ž This article includes 2 downloadable practice files ↓

⏱ 3 min readUpdated 27 September 2026

You do not need to become a software developer to get value from Python. If you already use filters, pivot tables and VLOOKUP, you understand most of what the pandas library does. This post walks through a real routine β€” load, clean, summarise, save β€” and shows the Excel equivalent of every line.

In this article
  1. Setup (once)
  2. 1. Load the file β€” like opening the workbook
  3. 2. Clean it β€” like TRIM, Find & Replace and Remove Duplicates
  4. 3. Filter β€” like AutoFilter
  5. 4. Summarise β€” like a pivot table
  6. 5. Lookup β€” like VLOOKUP / XLOOKUP
  7. 6. Save β€” to a new workbook with several sheets
  8. When is Python better than Excel?
  9. Try it yourself: step by step

Setup (once)

Install Python from python.org (tick β€œAdd to PATH”), then in a terminal:

pip install pandas openpyxl

1. Load the file β€” like opening the workbook

import pandas as pd

df = pd.read_excel("sales.xlsx", sheet_name="Data")
print(df.head())        # first 5 rows
print(df.shape)         # (rows, columns)

2. Clean it β€” like TRIM, Find & Replace and Remove Duplicates

df.columns = df.columns.str.strip().str.lower()          # tidy headers
df["region"] = df["region"].str.strip().str.title()        # TRIM + PROPER
df["amount"] = pd.to_numeric(df["amount"], errors="coerce")  # numbers stored as text
df = df.dropna(subset=["amount"]).drop_duplicates()
πŸ’‘ Numbers stored as text are the #1 problem with SAP and ERP exports. pd.to_numeric(..., errors="coerce") converts what it can and turns the rest into blanks you can inspect.

3. Filter β€” like AutoFilter

big_north = df[(df["region"] == "North") & (df["amount"] > 10000)]

4. Summarise β€” like a pivot table

pivot = pd.pivot_table(df, index="region", columns="product",
                       values="amount", aggfunc="sum", fill_value=0, margins=True)
print(pivot)

5. Lookup β€” like VLOOKUP / XLOOKUP

managers = pd.read_excel("sales.xlsx", sheet_name="Managers")   # region, manager
df = df.merge(managers, on="region", how="left")

merge with how="left" keeps every sales row and adds the manager β€” exactly what an XLOOKUP down the whole column does, but in one line and without #N/A surprises.

6. Save β€” to a new workbook with several sheets

with pd.ExcelWriter("summary.xlsx") as xl:
    pivot.to_excel(xl, sheet_name="Pivot")
    big_north.to_excel(xl, sheet_name="Big North", index=False)

When is Python better than Excel?

  • The same steps every week β€” a script never forgets a step.
  • Files too big for Excel to open comfortably (hundreds of thousands of rows).
  • Combining many files β€” see merge 100 Excel files with Python.

And when is Excel better? When someone needs to explore the data by eye, or when the output is a formatted report people will edit. Use both.

Try it yourself: step by step

  1. Download sales.xlsx and clean_and_summarise.py into the same folder.
  2. Open the workbook first: rows 2–7 have messy regions (β€œ north ”) and rows 8–11 have quantities stored as text β€” the problems the cleaning steps fix.
  3. Open a terminal in that folder (in File Explorer type cmd in the address bar) and run pip install pandas openpyxl once.
  4. Run python clean_and_summarise.py. The pivot prints on screen and summary.xlsx appears with two sheets.
  5. Change one line β€” e.g. aggfunc="sum" to aggfunc="mean" β€” run again and compare.

πŸ“Ž Practice files for this article

  • πŸ“—
    Messy sales workbookIncludes extra spaces and numbers-stored-as-text on purpose, so the cleaning steps have something to fix.
    ⬇ XLSX Β· 8 KB
  • 🐍
    Complete Python scriptRuns every step of the article end-to-end and writes summary.xlsx.
    ⬇ PY Β· 698 B

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.