So far, our SQL posts have read data that already sits in a database. Real work often starts earlier or ends later than that. Picture a shared folder with a few hundred monthly exports, where each file has slightly different column names. A database query cannot help with those files yet. Python covers these steps, so we first say what it is and then look at the jobs it handles best.
What is Python?
Python is a general-purpose programming language. We write instructions as plain text, and Python runs them from top to bottom. Its syntax reads close to English, so even a beginner can often follow a short script. Python is free, and it runs on Windows, macOS and Linux. A script works like a recipe card, because anyone who follows the same steps on the same ingredients gets the same dish.
Why analysts use Python
Analysts use Python for four kinds of jobs.
- Cleaning messy files, such as fixing column names, removing duplicate rows and standardising dates.
- Combining sources, such as CSV files, Excel sheets and database tables in one table.
- Repeating a report, which means running the same script on next month's data.
- Charts and statistics, since mature libraries already exist for both.
Python takes longer to set up than a spreadsheet, but once a script works, we can run it again on a new file in seconds. For a quick one-off total, Excel is still faster. We therefore choose by how big and how repeated the job is.
A first look at Python code
To see how it reads, we take four order amounts and ask two questions. What is the total, and which orders are 10 million or more?
amounts = [15000000, 8000000, 12000000, 20000000]
print(sum(amounts)) # 55000000
print(sum(amounts) / len(amounts)) # 13750000.0
big = [a for a in amounts if a >= 10000000]
print(big) # [15000000, 12000000, 20000000]The first two lines use built-in functions, sum and len. The last line is a list comprehension, which is a short way to keep only the items that pass a test. We do not need any extra library for this.
Python, SQL and Excel: which one for which job
These tools overlap, so the useful question is which one fits the job at hand.
| Job | First choice | Why |
|---|---|---|
| Read data that already sits in a database | SQL | The database does the filtering, so less data moves |
| Clean a messy export of 500,000 rows | Python | Rules are written once and applied to every row |
| Build a one-off chart for tomorrow's meeting | Excel | The fastest route for a small table |
| Repeat the same weekly report | Python | The same script runs on the new file |
Where pandas fits
Plain Python handles lists and numbers well. For tables with thousands of rows, analysts add a library called pandas. It gives Python a table object, the DataFrame, with rows and columns like a SQL table. We use it in pandas Basics: Read, Filter and Group Data in Python.


