Skip to content
Tutorials Center
My learning

Topics

Lesson 137 of 149 ยท Projects

Project: Data Report

Build a data report in Python from a CSV file. Clean messy rows, group and total the data, compute statistics, chart trends in text and save the finished report.

A lot of real-world programming looks like this: someone hands you a spreadsheet export and asks, "So, how are we doing?" In this project you'll answer that question for a small bike-rental company with three stations. Your program will read their CSV file, throw out the broken rows, total and compare the stations, compute statistics, spot the trend from month to month, and produce a clean text report saved to a file.

You'll use CSV files, data classes, defaultdict, the statistics module, dates, exceptions, pathlib and plenty of f-string formatting. As in the other projects, there are six stages, each one complete and runnable.

The data

The company exports one row per station per month: the month, the station name, the number of rentals and the revenue in dollars. A pilot station called Riverside was closed after a few weeks, and its rows are messy, as real data often is.

In this editor, each stage first writes the CSV text into a file called rentals.csv in the temporary in-memory file system, then reads it back like a real data file. On your computer, the file would already exist and you'd skip that setup step.

Stage 1: read the CSV file

csv.DictReader reads a CSV file and turns each row into a dictionary, using the header line as the keys. Always open CSV files with newline="", as the csv module expects:

Python Stage 1: reading rows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the last line: every value comes back as a string, even the numbers. CSV files have no types; that's our job. And the Riverside rows contain n/a and a negative number of rentals, which no amount of conversion will fix.

Stage 2: parse and clean

A Record data class gives each row real types: a date for the month, an int for rentals and a float for revenue. A parse_row() function converts one row and raises ValueError for anything invalid. The loader calls it for every row, keeps the good records and collects a note about every bad one, so problems are reported instead of silently dropped:

Python Stage 2: typed, validated records

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Some choices worth noting:

  • The record is frozen=True: once loaded, data can't be changed by accident. Analysis code should read data, not rewrite it.
  • date.fromisoformat("2026-01" + "-01") turns the month into a real date, so months sort correctly and can be formatted any way we like.
  • Validation lives in one function. If the company adds a rule tomorrow ("revenue can't exceed $10,000 per month"), there's one obvious place to put it.
  • Reporting rejected rows with line numbers lets someone fix the source data. Silently dropping rows is how reports end up quietly wrong.

Stage 3: totals by station

Now the analysis. Grouping records by station with a defaultdict collects each station's records in one pass. From each group we compute total rentals, total revenue, the average revenue per rental and each station's share of the company's revenue:

Python Stage 3: station totals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The table already tells a story: Old Town has the most rentals, but Park earns the most per ride, perhaps because people rent bikes there for longer. That's the kind of insight a report exists to surface.

Format specs do the heavy lifting: :>9, right-aligns in 9 characters with a thousands separator, :>12,.2f adds two decimal places, and :>8.1% multiplies by 100 and adds a percent sign.

Stage 4: monthly trend and statistics

Next, the trend over time. We total revenue per month (all stations together), then compute the change from each month to the next. The statistics module provides the mean, median and standard deviation, a measure of how spread out the values are:

Python Stage 4: month by month

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

New tools in this stage:

  • itertools.pairwise() yields neighboring pairs, (Jan, Feb), (Feb, Mar) and so on, which is exactly what a month-over-month comparison needs (see itertools and functools).
  • Dates format themselves in f-strings: {month:%b %Y} gives Jan 2026 and {best:%B} gives the full month name.
  • :+6.1% always shows a sign, so growth reads +12.3% and a drop reads -4.5%.
  • max(months, key=monthly.get) finds the month whose revenue is highest, the same trick as finding the top student in Writing Pythonic Code.

Stage 5: a text chart

Numbers in a table are precise, but a chart shows the shape at a glance. A horizontal bar chart needs nothing more than repeated characters. We scale each value against the largest one so the longest bar has a fixed width, and we use the block characters โ–ˆ and โ–Œ for a smoother look:

Python Stage 5: horizontal bar chart

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The label_width trick is a nested format spec: {label:>{label_width}} uses one variable inside the format of another, so the labels line up whatever their length. Dividing half_blocks with // and % (floor division and remainder) gives the number of full blocks and whether a half block is needed.

Stage 6: the complete report

Finally, we assemble everything into a program with a clear structure:

  1. load_records() reads and validates the file, returning good records and problems.
  2. summarize() computes every number and returns them in a Summary data class.
  3. render_report() turns a summary into text; it does no calculations.
  4. main() wires it together, prints the report, saves it to report.txt and exports the station totals to station_summary.csv for the spreadsheet fans.
Python Stage 6: the complete data report

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

That's a complete, real-world style data pipeline: load โ†’ validate โ†’ analyze โ†’ present โ†’ export. Each step is a separate function with typed inputs and outputs, which pays off in several ways:

  • You can test each step on its own. summarize() takes a list of records, so a unit test can pass in three hand-made records and check the totals, without any file.
  • You can swap the presentation. Want HTML or JSON output? Write render_html(summary) and leave the analysis untouched.
  • You can reuse the loader. Next month's file goes through exactly the same validation.

Running it on your computer: delete the RAW_CSV string and the setup line, put a real rentals.csv next to the script, and replace the final main() call with if __name__ == "__main__": main(). For much larger datasets, the third-party pandas library does grouping and statistics in a few lines, but everything it does builds on the ideas you practiced here.

Ideas to extend the report

  1. Command-line arguments. Use the argparse module so the input file and output folder can be passed as python report.py data.csv --out reports/.
  2. Per-station trends. Add a small table showing each station's best month and its growth from first to last month.
  3. Rentals per day. Divide each month's rentals by its number of days, found with calendar.monthrange(), so February isn't penalized for being short.
  4. An HTML version. Render the same Summary into an HTML table you can open in a browser.
  5. Handle a missing file. Catch FileNotFoundError in main() and print a friendly message instead of a traceback.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What type are the values that csv.DictReader returns for each field?

    CSV has no types, so every field arrives as a string. parse_row() converts them and rejects values that don't convert.
  2. 2. Why does the loader collect a message for each rejected row instead of silently skipping it?

    Silently dropped rows make reports quietly wrong. Listing them with line numbers keeps the report honest and the data fixable.
  3. 3. What does itertools.pairwise([a, b, c]) yield?

    pairwise yields overlapping neighbor pairs, which is perfect for comparing each month with the previous one.
  4. 4. What does the format spec in f"{0.123:+.1%}" produce?

    % multiplies by 100 and adds a percent sign, .1 keeps one decimal place, and + always shows the sign.

Summary

  • csv.DictReader turns rows into dictionaries of strings; a parse_row() function converts and validates them, and rejected rows are reported, not hidden.
  • Frozen data classes give records real types and protect them from accidental changes.
  • defaultdict groups data in one pass; statistics.mean, median and stdev describe it; itertools.pairwise() compares neighbors.
  • Format specs such as :>12,.2f, :+.1% and :%b %Y turn numbers and dates into a readable report, and repeated block characters make a text chart.
  • Splitting the program into load โ†’ analyze โ†’ present โ†’ export functions makes it testable, reusable and easy to extend.

Congratulations! You've gone from your first print() to classes, async code, tests and complete projects. One more project follows: a web API. Next: Project: JSON API with Flask.