Skip to content
KALANBLON
Show contents

Chapter 1 of 7 · 20 min

A dataset is a table

By the end of this chapter, you'll be able to

  • Describe a dataset: rows, columns, values
  • Recognise a CSV file and its header row
  • Read CSV data with the csv module
  • Convert text values into numbers to calculate with them

A dataset is a table

A dataset is a set of information organised as a table:

  • each row describes one thing: a delivery, a pupil, a sale;
  • each column describes a feature of that thing: the date, the village, the number of crates;
  • where they meet, you find a value.
datevillagecrates
2026-11-02Sikasso40
2026-11-02Kati25
2026-11-03Sikasso35

This is exactly what you would do in a notebook or a spreadsheet. The difference: Python can then do all the calculations for you.

The CSV format

To share a table between programs, the CSV format (comma-separated values) is used very often: a plain text file, one line per row of the table, with the values separated by commas. The first line, the header, gives the column names.

date,village,crates
2026-11-02,Sikasso,40
2026-11-02,Kati,25
2026-11-03,Sikasso,35

Almost every spreadsheet can save a table as CSV ("Save as", then "CSV"). It is the way your data gets into Python.

Reading a CSV with Python

The csv module from the standard library reads this format for you. csv.DictReader turns each line into a dictionary: a small record where each value is stored under the name of its column. You write row["village"] to read the value of the "village" column.

So that the example runs straight in your browser, the data is written inside the program, and io.StringIO makes it look like a file. With a real file, you would write open("deliveries.csv", encoding="utf-8") instead.

import csv
import io

data = """date,village,crates
2026-11-02,Sikasso,40
2026-11-02,Kati,25
2026-11-03,Sikasso,35
2026-11-03,Koutiala,30"""

rows = list(csv.DictReader(io.StringIO(data)))
print(len(rows), "deliveries")
print(rows[0])
print("Village of the 2nd delivery:", rows[1]["village"])
4 deliveries
{'date': '2026-11-02', 'village': 'Sikasso', 'crates': '40'}
Village of the 2nd delivery: Kati

rows is a list: a sequence of values numbered from 0. rows[0] is the first delivery, rows[1] the second.

The total is printed: 65 crates.

Step 7 of 7
Text description of the animation
  1. A tiny file with two deliveries and its header row.
  2. The text has three lines: the header, then Kati and Sikasso.
  3. The header is not a delivery: it gives the column names. Two records remain.
  4. We start a total at zero.
  5. First record: we convert the text "25" into a number and add it.
  6. Second record: 40 more crates.
  7. The total is printed: 65 crates.

Mini-lab: your first data readingPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: total crates

With the data below, print the total number of crates delivered, then the number of crates delivered by Sikasso only.

data = """date,village,crates
2026-11-02,Sikasso,40
2026-11-02,Kati,25
2026-11-03,Sikasso,35
2026-11-03,Koutiala,30"""
Hint 1
Go through the rows with a for loop, and add int(row["crates"]) to a total that starts at 0.
Hint 2
For Sikasso, keep a second total, and only add when row["village"] == "Sikasso".
Show the solution and common mistakes
import csv
import io

data = """date,village,crates
2026-11-02,Sikasso,40
2026-11-02,Kati,25
2026-11-03,Sikasso,35
2026-11-03,Koutiala,30"""

total = 0
total_sikasso = 0
for row in csv.DictReader(io.StringIO(data)):
    crates = int(row["crates"])
    total = total + crates
    if row["village"] == "Sikasso":
        total_sikasso = total_sikasso + crates

print("Total:", total, "crates")
print("Sikasso:", total_sikasso, "crates")
Total: 130 crates
Sikasso: 75 crates

Common mistakes:

  • Forgetting int(): the total becomes a long string of digits glued together, or Python refuses to add 0 and a text (TypeError).
  • Writing "sikasso" without a capital letter: for Python, "Sikasso" and "sikasso" are two different texts, and the total stays at 0.

Summary

  1. 1A dataset is a table: one row per thing observed, one column per feature.
  2. 2A CSV file is a table as text: one line per row, commas between values, and a header line.
  3. 3csv.DictReader turns each line into a dictionary: you read a value with row["column name"].
  4. 4In a CSV, everything is text: convert with int() or float() before calculating.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.