Day 12 - Extracting columns
Skills: None
Pre-reading: DRAFT:5.1.1, 5.1.2, 5.1.3, 5.1.4.1
Intro (25 mins)
- Consider you have a small cafe that tracks daily sales, where each row has the
number of drinks sold:
cafe-data =
table: day, drinks-sold
row: "Mon", 45
row: "Tue", 30
row: "Wed", 55
row: "Thu", 40
row: "Fri", 60
end - There are many basic statistical questions we might want to ask -- e.g., what is the maximum number of drinks sold, the average sold per day, or the total sold.
- We can extract single columns from tables, and then use standard
operations from two different software libraries --
math
andstatistics
-- to operate over those columns.import math as M
import statistics as S
sales = cafe-data.get-column("drinks-sold")
M.max(sales) # maximum sales
S.mean(sales) # average sales
M.sum(sales) # total sold - Here,
import math as M
declares that you want to use code that comes from Pyret'smath
library (https://pyret.org/docs/latest/math.html), and in order to not get confused between the functions provided from it and the functions available by default or defined by you, theas M
gives the library a short name. This means that any functionfunction-name
from themath
library is availabe asM.function-name
. The choiceM
is arbitrary -- but convention is to use capital letters, and usually just one or two. - If you aren't worried about mixing up functions from the library, you can
instead write:
This adds all the functions from the
include math
math
library to the current program directory with no change of name, so we could write the above alternately as:This is more concise but possibly a little harder to read, as someone has to figure out thatinclude math
include statistics
sales = cafe-data.get-column("drinks-sold")
max(sales) # maximum sales
mean(sales) # average sales
sum(sales) # total soldmax
andsum
from frommath
butmean
comes fromstatistics
, which is not obvious from reading the code in the way it was when we used theimport
form. - A column is a type of data called a
list
-- if we print it out we can see that it looks like:This shows at the interactions as:cafe-data.get-column("drinks-sold")
[list: 45, 30, 55, 40, 60]
- Lists have order, but no column header, and unlike tables, only a single value per entry
- We can define lists directly using the above syntax:
sample-list = [list: 10, 20, 30]
empty-list = [list: ] - And lists can have any type of data in them. On lists of numbers we can use
functions from
math
andstatistics
on them.
Class Exercise (30 mins)
FIXME / TODO
Wrap-Up (5 mins)
- Columns can be extracted from tables -- these are lists.
- Many built in functions work on lists of values.