Skip to main content

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 and statistics -- 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's math 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, the as M gives the library a short name. This means that any function function-name from the math library is availabe as M.function-name. The choice M 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:
    include math
    This adds all the functions from the math library to the current program directory with no change of name, so we could write the above alternately as:
    include math
    include statistics

    sales = cafe-data.get-column("drinks-sold")
    max(sales) # maximum sales
    mean(sales) # average sales
    sum(sales) # total sold
    This is more concise but possibly a little harder to read, as someone has to figure out that max and sum from from math but mean comes from statistics, which is not obvious from reading the code in the way it was when we used the import form.
  • A column is a type of data called a list -- if we print it out we can see that it looks like:
    cafe-data.get-column("drinks-sold")
    This shows at the interactions as:
    [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 and statistics 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.