This is a free preview. Enrol on the course to save your progress and open every lesson.

Lesson 3 of 30 · 15 min

Data with plain Python first

Why pandas exists

You can analyse a CSV with plain Python and the csv module from Python Foundations. Let's add up a day's sales:

import csv

total = 0
orders = 0
with open("data/cafe_sales_2025.csv", newline="") as file:
    for row in csv.DictReader(file):
        if row["date"] != "01/01/2025":
            continue
        if row["quantity"] == "":
            continue
        price = float(row["unit_price"].replace("$", ""))
        total += int(row["quantity"]) * price
        orders += 1

print(f"1 January: {orders} order lines, ${total:.2f}")

Output:

1 January: 14 order lines, $113.80

It works — but look how much code it took for one simple question, and we already had to handle two problems (prices with $ signs, and blank quantities). Now imagine "total sales for every month, by category, without the duplicates".

The same question with pandas

import pandas as pd

df = pd.read_csv("data/cafe_sales_2025.csv")
day = df[df["date"] == "01/01/2025"].dropna(subset=["quantity"])
price = day["unit_price"].str.replace("$", "", regex=False).astype(float)
print(f"1 January: {len(day)} order lines, ${(day['quantity'] * price).sum():.2f}")

Output:

1 January: 14 order lines, $113.80

Same answer, and once you know pandas, it reads almost like English. The rest of this course teaches you how.

Try it

Do the Module 1 exercise in the notebook: count the cash payments with plain Python.