Class 13 - Extracting columns
Date: October 7, 2026
For all work with tables, refer to the Tables page in the menu at the top of the page!
Skills: None
Notes (review before class)
Questions About a Column
So far, table operations have worked by rows and produced tables. But many questions are basic statistics (maximum, minimum, average, median, ...) over a single column, possibly after filtering rows: the largest ticket order, the average or most common number of tickets, the most used discount code. We'll use a cleaned copy of the events data from Class 11 and Class 12:
cleaned_data_raw = load_table("https://raw.githubusercontent.com/neu-pdi/cs2000-public-resources/refs/heads/main/static/support/dcic-events-cleaned.csv")
cleaned_data = cleaned_data_raw.transform_column("Num Tickets", int)
Extracting a Column
select_columns produces a table, and no table method does these computations:
cleaned_data.select_columns(["Num Tickets"])
The column method produces just the values, as a list:
cleaned_data.column("Num Tickets") # -> [2, 1, 5, 0, 3, 10, 3]
Lists
- A list is a sequence of values outside of a table. Like a one-column table, it has an order (first, ..., last), and its elements should all have the same type.
- Unlike a column, a list is anonymous (it has no name): the program, not
the data, says what it represents, as with a number like
3(an age? a play count?). Tables, with their column names, are the exception. - So we can misinterpret a list, but one operation works on any list, whatever it represents (genericity). Some need numbers (average); others, anything that can be compared, like strings (maximum).
- Lists are written in square brackets (as table rows have been since
Class 7), and can be named.
[]is the empty list.
[1, 2, 3]
["This", "is", "a", "list", "of", "words"]
shopping_list = ["muesli", "fiddleheads"]
Statistics on Lists of Numbers
max, min, and sum are built into Python; more come from Python's
statistics library, which we import (as we did math on
Class 9):
import statistics as S
ticket_counts = cleaned_data.column("Num Tickets")
max(ticket_counts) # largest number in a list
sum(ticket_counts) # sum of numbers in a list
S.mean(ticket_counts) # mean (average) of numbers in a list
S.median(ticket_counts) # median of numbers in a list
import statistics as Snames the libraryS(our choice), soS.meanis itsmean, which can't be confused with built-in functions or yours.- With no
as(likeimport math), use the full name:statistics.mean. from statistics import *adds the library's functions to the directory under their own names (mean(ticket_counts)). We usually avoid it: you can't tell where a name came from, and a library name can hide one of yours.- Try: find the function in the
statisticsdocumentation that gives the most common number of tickets.
Recap (25 mins)
A small cafe records the number of drinks it sold each day, and wants to know which days were busier than average. Read through the program:
import statistics as S
cafe_data = table(
["day", "drinks-sold"],
[
["Mon", 45],
["Tue", 30],
["Wed", 55],
["Thu", 40],
["Fri", 60],
],
)
sales = cafe_data.column("drinks-sold")
average_sales = S.mean(sales)
def busier_than_average(drinks: int) -> bool:
"""whether a day with drinks sold was busier than the average day"""
return drinks > average_sales
def test_busier_than_average():
assert busier_than_average(60)
assert not busier_than_average(30)
assert not busier_than_average(46)
busy_days = cafe_data.filter(lambda r: busier_than_average(r["drinks-sold"]))
- What is
sales? What does it lack thatcafe_data.select_columns(["drinks-sold"])has? - What is
average_sales? Why is there a test with46? Which days are inbusy_days? - If the first line were
import statistics, which line would have to change, and how?
Class Exercise (30 mins)
-
Using the cafe data from the intro, extract the "day" column and determine which day appears first alphabetically using
min. -
Create a list for the number of drinks, and compute the total drinks sold across all days using
sum. -
Create a new table of student quiz scores:
quiz_scores = table(
["student", "quiz1", "quiz2", "quiz3"],
[
["Alice", 85, 92, 78],
["Bob", 90, 88, 95],
["Charlie", 78, 85, 82],
["Diana", 95, 90, 88],
],
)Extract each quiz column and calculate the class average for each quiz using
S.mean. Which quiz had the highest average? -
Create a list directly using the syntax
[12, 8, 15, 22, 5, 18]and use built-in functions to find the minimum, maximum, and sum. What's the range (difference between max and min)? -
Load the employees dataset from https://data.boston.gov/dataset/employee-earnings-report using the CSV URL (as in Class 9). Transform the "REGULAR" column to numbers using the
earnings_to_numberhelper from Class 9, extract it as a list and calculate the average regular salary across all employees.
Wrap-Up (5 mins)
- Columns can be extracted from tables -- these are lists.
- Many built in functions work on lists of values.