THINK FIRST·CODE LATER

← All labs

Classify the attributes of a dataset

Problem

Read a CSV from standard input and classify every column:

  • numeric column: key if its name ends with id (case-insensitive) and its values are unique, otherwise quantitative;
  • text column: temporal if every non-missing value parses as a date-time (pd.to_datetime(..., errors="coerce", format="mixed")); otherwise key if the name ends with id and values are unique; otherwise ordinal if all lower-cased values belong to {low, medium, high, good, moderate, unhealthy, small, large}; otherwise categorical.

Print 4 items x 5 attributes, then per column pm25: quantitative (Q), 3 distinct, 1 missing where the code is N for key and categorical, O, Q or T.

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

sensor_id,district,timestamp,pm25,level
101,Lucheng,2026-03-01 08:00,42,moderate
102,Ouhai,2026-03-01 08:00,12,good
103,Longwan,2026-03-01 08:00,,good
104,Lucheng,2026-03-01 09:00,88,unhealthy
What your program must print for v2-1-data-1.csv
4 items x 5 attributes
sensor_id: key (N), 4 distinct, 0 missing
district: categorical (N), 3 distinct, 0 missing
timestamp: temporal (T), 2 distinct, 0 missing
pm25: quantitative (Q), 3 distinct, 1 missing
level: ordinal (O), 3 distinct, 0 missing

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

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

Get-Content v2-1-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-1-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.