THINK FIRST·CODE LATER

← All labs

A data-quality report before the first chart

Problem

Read sensor readings (sensor_id,district,timestamp,pm25) from standard input and print a quality report:

rows read: 9
exact duplicate rows: 1
missing pm25: 1
unparseable timestamps: 1
out-of-range pm25 (<0 or >1000): 2
rows kept: 4 of 9 (44%)
mean pm25 by district: Longwan 20.0, Lucheng 46.0, Ouhai 30.0

Order: drop exact duplicates first; then count missing PM2.5; parse timestamps with errors="coerce" and count failures; count out-of-range values; normalize district names with str.strip().str.title(); keep rows with 0 ≤ pm25 ≤ 1000 and a valid timestamp. The percentage uses the original row count (format :.0%); means are rounded to 1 decimal, districts in alphabetical order.

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.

v2-3-data-1.csv

sensor_id,district,timestamp,pm25
101,Lucheng,2026-03-01 08:00,42
101,Lucheng,2026-03-01 08:00,42
102, ouhai,2026-03-01 08:00,30
103,Longwan,2026-03-01 08:00,
104,Lucheng,2026-03-01 09:00,-5
105,Ouhai,not a date,33
106,LUCHENG,2026-03-01 10:00,50
107,Longwan,2026-03-01 10:00,2400
108,Longwan,2026-03-01 11:00,20
What your program must print for v2-3-data-1.csv
rows read: 9
exact duplicate rows: 1
missing pm25: 1
unparseable timestamps: 1
out-of-range pm25 (<0 or >1000): 2
rows kept: 4 of 9 (44%)
mean pm25 by district: Longwan 20.0, Lucheng 46.0, Ouhai 30.0

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 < v2-3-data-1.csv

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

Get-Content v2-3-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("v2-3-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.