Tables of data: the table mod
mods/table.luma works with tables of data the way pandas’ DataFrame does: read one from CSV, choose rows and columns, sort, group, join, add columns, sum and average, and write it back. It keeps each column as a list of one type, so it is quick: in a built program, 200,000 rows are read in 0.2 seconds and grouped, sorted or filtered in under 0.1. Copy it next to your program and import it.
import "table"
t: table.table = table.load(file("sales.csv"))
print(t.show())Output:
city price qty note
Tallinn 2.5 3 a
Tartu 10.0 1
Tallinn 4.0 x, y
Narva 1.25 8 b
Tartu 2 c
[5 rows × 4 columns]Columns, types and missing cells
Each column has one type, guessed from its values when a table is read: all ints make an int column, ints and floats a float one, anything else a str one. An empty cell is missing: the statistics pass it over, show leaves it empty, and r.has(name) asks for it.
| Method | What it gives |
|---|---|
table.load(f), table.load(f, ";") | a table from a CSV file whose first row names the columns |
table.parse(text) | the same from CSV text |
t.len(), t.names(), t.kind(name) | how many rows; the columns’ names; a column’s type: "int", "float" or "str" |
t.ints(name), t.floats(name), t.strs(name) | a column’s values as a list (a missing one as 0, 0.0 or “”); floats of an int column too, strs of any |
t.head(n), t.tail(n) | the first or last n rows |
t.pick(names), t.drop(names) | a table of these columns, or without them |
t.rename(old, new) | gives a column another name |
t.show() | the table as aligned text: all rows if there are 20 or fewer, else the first and last 5 |
t.csv(), t.save(f) | the table as CSV text, or written to a file, with its header row |
Rows: filter and add
filter and add give a function each row, a table.row. It reads a cell by the column’s name and the type it wants:
cheap: table.table = t.filter(fn(r: table.row): bool => r.has("price") && r.float("price") < 5.0)
t.add("total", fn(r: table.row): json {
if r.has("price") && r.has("qty") {
return r.float("price") * r.float("qty")
}
return null
})r.int(name) | an int column’s cell |
r.float(name) | a float column’s cell, or an int one’s |
r.str(name) | any cell, as text |
r.has(name) | whether the cell isn’t missing |
t.filter(keep) | a table of the rows keep says yes to |
t.add(name, f) | adds a column (or replaces one) of what f gives each row: ints, floats or strs, null for missing; its type is the widest of them |
Reading a missing cell raises table.MISSING: row 3 has no qty. A column read as another type raises table.COLUMN: price is a float column, not int.
Statistics, sorting, groups
| Method | What it gives |
|---|---|
t.sum(name), t.mean(name), t.min(name), t.max(name) | of a number column’s cells that aren’t missing, a float |
t.count(name) | how many cells of a column aren’t missing |
t.sort(name), t.sort(name, true) | the rows by a column, smallest first or largest first; equal ones keep their order, missing ones go last |
t.unique(name) | a column’s values, each once, in the order they come |
t.describe() | a table of every number column’s count, mean, min and max |
t.group(name) | the rows grouped by a column’s values, for: |
.sum(col), .mean(col), .min(col), .max(col) | a table of each group’s key and the statistic of col, named col |
.count() | a table of each group’s key and its number of rows, count |
print(t.group("city").sum("price").sort("price", true).show())Output:
city price
Tartu 10.0
Tallinn 6.5
Narva 1.25
[3 rows × 2 columns]Groups come in the order their keys first appear. mean of a group gives a float column; sum, min and max of an int column an int one.
Joining
t.join(other, name) gives the rows of t with the rows of other whose name column has the same value, other’s columns after t’s. t.join(other, name, true) keeps the rows of t that have no match too, with other’s cells missing. A column of other with a name t has already gets _2 after its name.
people: table.table = table.parse("city,people\nTallinn,450000\nTartu,95000\n")
print(t.join(people, "city").pick(["city", "price", "people"]).show())Details
- The results are those of pandas: checked on 3,000 rows with missing cells (sums, means, groups, sorting, joins, filtering).
- A column whose header is empty is named
columnand its number; a name twice gets_and its number. print(t)would show the table’s inner lists: useprint(t.show()).- Errors:
table.COLUMN(100: no such column, or one of another type),table.MISSING(101),table.EMPTY(102:mean,minormaxof a column with no values).
Not available yet
- Date columns (they stay
str; convert them inadd), pivot tables, rolling windows, plots, and data larger than memory.