THINK FIRST·CODE LATER

← All labs

Your first chart specification with Altair

Problem

CityPulse gives you daily PM2.5 readings: a CSV on standard input with columns date,district,pm25.

Build an Altair chart titled "Daily PM2.5 by district": mark_line(point=True), x = date (temporal), y = mean(pm25) (quantitative), colour = district (nominal), with a tooltip. Do not render it — inspect its specification with chart.to_dict() and print:

mark: line
x: date as temporal
y: mean(pm25) as quantitative
color: district as nominal
rows: 12 | districts: 3 | days: 4
highest mean: Lucheng (51.5)
lowest mean: Longwan (28.5)

(aggregated fields are written mean(pm25); means with 1 decimal.)

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v1-2-data-1.csv

date,district,pm25
2026-03-01,Lucheng,42
2026-03-01,Ouhai,35
2026-03-01,Longwan,28
2026-03-02,Lucheng,55
2026-03-02,Ouhai,40
2026-03-02,Longwan,31
2026-03-03,Lucheng,61
2026-03-03,Ouhai,38
2026-03-03,Longwan,25
2026-03-04,Lucheng,48
2026-03-04,Ouhai,39
2026-03-04,Longwan,30
What your program must print for v1-2-data-1.csv
mark: line
x: date as temporal
y: mean(pm25) as quantitative
color: district as nominal
rows: 12 | districts: 3 | days: 4
highest mean: Lucheng (51.5)
lowest mean: Longwan (28.5)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v1-2-data-1.csv

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v1-2-data-1.csv | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v1-2-data-1.csv", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.