Class 7 - Introduction to tables
Date: September 23, 2026
⚠️Homework
Homework 2 due September 27, 2026
🆕New Skills
First week for skills: 0
Reference
For all work with tables, refer to the Tables page in the menu at the top of the page!
Skills: 3
Notes (review before class)
Tables
- A table has rows that share the same attributes, its columns: a workout journal, a recipe index, a library catalog.
- Tables are values: they can be named, passed to functions, and returned. PLL provides them; there's nothing to import.
- Code that works with tables follows their structure of rows and columns.
Literal Tables
A literal table is written out with table, given the column names, in
order, and then a list of rows:
people = table(
["name", "age"],
[
["Alicia", 30],
["Meihui", 40],
["Jamal", 25],
],
)
- Each row has one value per column, in order. (Try: remove a value or a comma, and read the error.)
- The directory gets just
people, for the whole table: no column names. - Tables are equal only if their columns and rows are in the same order. A
test function can check a fact, rather than a function (
!=is "not equal"):
def test_column_order_matters():
assert people != table(
["age", "name"],
[[30, "Alicia"], [40, "Meihui"], [25, "Jamal"]],
)
Loading Tables from CSV Files
load_tableloads a CSV ("comma-separated values") file, which any spreadsheet can save, from a URL or a file in your program's folder:load_table("riders.csv"). The column names come from its first line.- Every value loaded from a CSV is a string, even
"4". Convert numeric columns, into a new table, withtransform_column(Class 9) andint("4"to4) orfloat("4.5"to4.5):riders = riders_raw.transform_column("riders", int). - Table operations like this are methods (like the string methods of Class 2): written after the table, with a dot.
Rows and Cells
t.row(n) is row n, counting from 0 at the top (so the last is one less than
the number of rows). A row is a dict: a column's name, a string, in square
brackets gives its value.
shuttle = table(
["month", "riders"],
[
["Jan", 1123],
["Feb", 1045],
["Mar", 1087],
["Apr", 999],
],
)
shuttle.row(2) # -> {'month': 'Mar', 'riders': 1087}
shuttle.row(2)["riders"] # -> 1087
march_row = shuttle.row(2)
march_row["riders"] # -> 1087
- Values work in any computation:
shuttle.row(2)["riders"] >= 1000. - Without quotes,
shuttle.row(2)[riders]is an error:ridersisn't in the directory. - Only the named version adds an entry,
march_row, to the directory. - Running a table's name shows it (its first rows, if it is long).
Summarizing Columns
sum, mean, min, and max give a column's total, average, smallest, and
largest value (more in Tables): shuttle.max("riders") is 1123.
Recap (15 mins)
A workout journal is small enough to type in as a table; a recipe index is a CSV file on the web. Read through the program:
workouts = table(
["date", "activity", "duration"],
[
["2025-04-01", "Running", 30],
["2025-04-02", "Yoga", 45],
["2025-04-03", "Cycling", 60],
],
)
def test_row_order_matters():
assert workouts != table(
["date", "activity", "duration"],
[
["2025-04-03", "Cycling", 60],
["2025-04-01", "Running", 30],
["2025-04-02", "Yoga", 45],
],
)
second_workout = workouts.row(1)
second_activity = second_workout["activity"]
average_duration = workouts.mean("duration")
recipes_raw = load_table("https://raw.githubusercontent.com/neu-pdi/cs2000-public-resources/refs/heads/main/static/support/5-recipes.csv")
recipes_with_servings = recipes_raw.transform_column("servings", int)
recipes = recipes_with_servings.transform_column("prep time", int)
average_prep_time = recipes.mean("prep time")
- What are
second_activityandaverage_duration? What expression would give the date of the first workout? - Why does
test_row_order_matterspass? - Why does the program compute
average_prep_timefromrecipes, rather than fromrecipes_raw? How are those two tables different?
Class Exercise (40 mins)
- Browse and pick a dataset on the London DataStore. Once you found a dataset to work with, open the page for details. Scroll down until you see a data set in CSV format. Right-click the "Download" button so you can copy (not download) the link; PLL will download it. Use the copied link (URL) with
load_tablein your code to create a table from it. (If PLL reports that it could not reach the URL, the site may not allow other sites to read its data. In that case, download the CSV, drag it into yourcs2000-scratchrepository in the Explorer on the left, and giveload_tableits file name instead.) - Look at the columns PLL loaded: which of them should be converted to numbers? Use
transform_columnto do that for one of them. - Use the interactions panel,
.row, and square brackets to explore the data a little bit. - Check the total number of rows with
table_identifier.length(). What happens if you try to dotable_identifier.row(M)forMthat is greater than the total number of rows? - Similarly, try extracting a column that doesn't exist.
Wrap-up (5 mins)
- Tables are an extremely common and powerful form of data. Today we just learned how to look at them; in the upcoming days we will see how to program with them!