Reference · Vectors
vec.column
vec.column(list | null, str | int) -> listWhat it does
One column out of a list of rows. With a string key, each row is a map and the result is
that field of every row, in order; with an int, each row is a list and the result is
that item of every row. A row without the field or the position gives null there. It
turns database rows into a list of numbers for stats and vec.
Examples
let orders = [{ name: "ann", total: 12.5 }, { name: "bob", total: 30 }]print(vec.column(orders, "total"))print(vec.column(orders, "name"))print(vec.column([[1, 2], [3, 4]], 1))[12.5, 30]["ann", "bob"][2, 4]Edge cases
- A missing field or a position past the row gives
nullin that place; filter the nulls out before arithmetic. - A row of the wrong kind (a list under a string key, a map under an int, a plain
value) gives
nulltoo, not an error. - A negative position is
null, not counted from the end. - A string key reads maps only: an int key does not look up
"0"in a map. - The empty list, or
null, gives the empty list.
let orders = [{ name: "ann", total: 12.5 }, { name: "bob", total: 30 }, { name: "cy" }]print(vec.column(orders, "total"), vec.column(orders, "missing"))print(vec.column([[1, 2], [3, 4], [5]], 1), vec.column([[1, 2]], -1))print(vec.column([{ a: 1 }, [1, 2], 7], "a"), vec.column([{ a: 1 }, [1, 2], 7], 0))print(vec.column([{ "0": "z" }], 0))print(vec.column([], "x"), vec.column(null, 0))[12.5, 30, null] [null, null, null][2, 4, null] [null][1, null, null] [null, 1, null][null][] []Errors
A key that is neither a string nor an int, or rows that are not a list, are runtime errors, not error values.
print(vec.column([{ a: 1 }], null))line 1: `vec.column` expects a str or int key as argument 2, got nullprint(vec.column("rows", "a"))line 1: `vec.column` expects a list as argument 1, got strChaining
rows | vec.column("total") reads well in a pipeline, with the statistics after it:
let orders = [{ name: "ann", total: 12.5 }, { name: "bob", total: 30 }, { name: "cy" }]print(orders | vec.column("total") | filter((t) => t != null) | sum)print(orders | vec.column("name") | join(", "))42.5ann, bob, cySee also
vec.transpose, map, stats.describe, vec.matmul
Thread 0 posts
On vec.column(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo