AI Hints
Home›Data Science Tutorial›Reliable Analysis
Data Science · Lesson 71 of 101

Automated Reports

Your Monday, back.

⏱ 2 min read▶ Video at 2:01:33🎓 Free
Watch this lesson in the Data Science courseWatch on YouTube ↗Full Data Science course

Each Monday morning, you pull the numbers, paste them into a slide, and write the same three sentences. It takes two hours. That's a hundred hours a year on something a script can do before you've even woken up.

Automated Reports
From the video: Automated Reports
  • Pull numbers, paste, repeat
  • Two hours every Monday
  • A hundred hours a year

A weekly summary

This script finds the latest date, and splits off this week and last week. It works out the change, the most profitable cafe, and the one with the weakest change. Then it writes the summary in plain English. Run it. That's a report that a manager can read in ten seconds. Schedule it to run early each Monday, and email the result. Notice that we didn't hard code any dates, so that it just keeps working every week.

report.pypython
import pandas as pd

sales = pd.read_csv("sales.csv", parse_dates=["date"])
last = sales["date"].max()
this = sales[sales["date"] > last - pd.Timedelta(days=7)]
prev = sales[(sales["date"] <= last - pd.Timedelta(days=7)) & (sales["date"] > last - pd.Timedelta(days=14))]

change = this["revenue"].sum() / prev["revenue"].sum() - 1
best = this.groupby("city")["profit"].sum().idxmax()
worst = (this.groupby("city")["revenue"].sum() / prev.groupby("city")["revenue"].sum() - 1).idxmin()

print(f"Weekly report to {last.date()}")
print(f"Revenue: ${this['revenue'].sum():,.0f} ({change:+.1%} on last week)")
print(f"Most profitable cafe: {best}")
print(f"Weakest change: {worst}")
Output
Weekly report to 2025-12-31
Revenue: $82,345 (+1.0% on last week)
Most profitable cafe: Osaka
Weakest change: Milan

Scheduling it

To run a script on a timetable, one simple way is cron. Each entry is a schedule and a command. This one means six in the morning, every Monday. Larger teams use tools such as Airflow or GitHub Actions, which also retry, and alert you when something fails. And remember, a report that fails silently is a dangerous thing, so always send an alert on failure.

terminal
echo '0 6 * * 1  python /home/analyst/report.py'
0 6 * * 1  python /home/analyst/report.py

Summary

Automate anything that you do twice. Next, the habits which make your work trustworthy, a year from now.

Key takeaways
  • Automate repeated reports
  • Never hard code dates
  • Alert on failure
Next up: Reproducible Analysis — can you do it again?