
π This article includes 2 downloadable practice files β
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
- Setup (once)
- 1. Load the file β like opening the workbook
- 2. Clean it β like TRIM, Find & Replace and Remove Duplicates
- 3. Filter β like AutoFilter
- 4. Summarise β like a pivot table
- 5. Lookup β like VLOOKUP / XLOOKUP
- 6. Save β to a new workbook with several sheets
- When is Python better than Excel?
- 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()
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
- Download sales.xlsx and clean_and_summarise.py into the same folder.
- 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.
- Open a terminal in that folder (in File Explorer type
cmdin the address bar) and runpip install pandas openpyxlonce. - Run
python clean_and_summarise.py. The pivot prints on screen and summary.xlsx appears with two sheets. - Change one line β e.g.
aggfunc="sum"toaggfunc="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.