Table.fmt()

Attach a formatting function to a column.

Usage

Source

Table.fmt(
    column,
    fn,
)

Parameters

column: str

The column to format.

fn: Callable[[Any], str]
A function that takes a single value and returns a string. Missing values (None) are rendered as empty cells and are never passed to fn.

Returns

Table
A new table; the original is not modified.

Examples

Format the price column as US dollars:

from tinytab import Table, fmt_currency

tbl = Table({"item": ["coffee", "bagel"], "price": [3.5, 2.25]})
tbl.fmt("price", fmt_currency)
itemprice
coffee$3.50
bagel$2.25

Any function that takes a value and returns a string works as a formatter:

tbl.fmt("price", lambda x: f"{x:.1f} USD")
itemprice
coffee3.5 USD
bagel2.2 USD