Column Labels
The table's Column Labels part contains, at a minimum, columns and their column labels. The last example had a single column: size. Just as in the Stub, we can create groupings called spanner labels that encompass one or more columns.
To better demonstrate how Column Labels work and are displayed, let's use an input data table with more columns. In this case, that input table will be airquality. It has the following columns:
Ozone: mean ground-level ozone in parts per billion by volume (ppbV), measured between 13:00 and 15:00Solar_R: solar radiation in Langley units (cal/m2), measured between 08:00 and noonWind: mean wind speed in miles per hour (mph)Temp: maximum daily air temperature in degrees Fahrenheit (°F)Month,Day: the numeric month and day of month for the record
We know that all measurements took place in 1973, so a year column will be added to the dataset before it is passed to the GT class.
import tabelline, gtdata
import std/sequtils
let airqualityMini = airquality().head(10)
airqualityMini.addColumn("Year", toValues(newSeqWith(10, 1973)))
echo airqualityMini.tabella
┌───────┬─────────┬──────┬──────┬───────┬─────┬──────┐ │ Ozone │ Solar_R │ Wind │ Temp │ Month │ Day │ Year │ ╞═══════╪═════════╪══════╪══════╪═══════╪═════╪══════╡ │ 41 │ 190 │ 7.4 │ 67 │ 5 │ 1 │ 1973 │ │ 36 │ 118 │ 8.0 │ 72 │ 5 │ 2 │ 1973 │ │ 12 │ 149 │ 12.6 │ 74 │ 5 │ 3 │ 1973 │ │ 18 │ 313 │ 11.5 │ 62 │ 5 │ 4 │ 1973 │ │ │ │ 14.3 │ 56 │ 5 │ 5 │ 1973 │ │ 28 │ │ 14.9 │ 66 │ 5 │ 6 │ 1973 │ │ 23 │ 299 │ 8.6 │ 65 │ 5 │ 7 │ 1973 │ │ 19 │ 99 │ 13.8 │ 59 │ 5 │ 8 │ 1973 │ │ 8 │ 19 │ 20.1 │ 61 │ 5 │ 9 │ 1973 │ │ │ 194 │ 8.6 │ 69 │ 5 │ 10 │ 1973 │ └───────┴─────────┴──────┴──────┴───────┴─────┴──────┘
from great_tables import GT, html
from great_tables.data import airquality
airquality_mini = airquality.head(10).assign(Year = 1973)
airquality_mini| Ozone | Solar_R | Wind | Temp | Month | Day | Year | |
|---|---|---|---|---|---|---|---|
| 0 | 41.0 | 190.0 | 7.4 | 67 | 5 | 1 | 1973 |
| 1 | 36.0 | 118.0 | 8.0 | 72 | 5 | 2 | 1973 |
| 2 | 12.0 | 149.0 | 12.6 | 74 | 5 | 3 | 1973 |
| 3 | 18.0 | 313.0 | 11.5 | 62 | 5 | 4 | 1973 |
| 4 | NaN | NaN | 14.3 | 56 | 5 | 5 | 1973 |
| 5 | 28.0 | NaN | 14.9 | 66 | 5 | 6 | 1973 |
| 6 | 23.0 | 299.0 | 8.6 | 65 | 5 | 7 | 1973 |
| 7 | 19.0 | 99.0 | 13.8 | 59 | 5 | 8 | 1973 |
| 8 | 8.0 | 19.0 | 20.1 | 61 | 5 | 9 | 1973 |
| 9 | NaN | 194.0 | 8.6 | 69 | 5 | 10 | 1973 |
library(gt)
library(dplyr)
airquality_mini <- airquality |> slice_head(n = 10) |> mutate(Year = 1973L)
airquality_miniOzone Solar.R Wind Temp Month Day Year 1 41 190 7.4 67 5 1 1973 2 36 118 8.0 72 5 2 1973 3 12 149 12.6 74 5 3 1973 4 18 313 11.5 62 5 4 1973 5 NA NA 14.3 56 5 5 1973 6 28 NA 14.9 66 5 6 1973 7 23 299 8.6 65 5 7 1973 8 19 99 13.8 59 5 8 1973 9 8 19 20.1 61 5 9 1973 10 NA 194 8.6 69 5 10 1973
Adding Column Spanners
Let's organize the time information under a Time spanner label, and put the other columns under a Measurement spanner label. We can do this with the GT.tab_spanner() method.
let gtAirquality = tabella(airqualityMini):
tabHeader "New York Air Quality Measurements",
"Daily measurements in New York City (May 1-10, 1973)"
tabSpanner "Time", ["Year", "Month", "Day"]
tabSpanner "Measurement", ["Ozone", "Solar_R", "Wind", "Temp"]
nbTable gtAirquality
| New York Air Quality Measurements | ||||||
|---|---|---|---|---|---|---|
| Daily measurements in New York City (May 1-10, 1973) | ||||||
| Measurement | Time | |||||
| Ozone | Solar_R | Wind | Temp | Year | Month | Day |
| 41 | 190 | 7.4 | 67 | 1973 | 5 | 1 |
| 36 | 118 | 8.0 | 72 | 1973 | 5 | 2 |
| 12 | 149 | 12.6 | 74 | 1973 | 5 | 3 |
| 18 | 313 | 11.5 | 62 | 1973 | 5 | 4 |
| 14.3 | 56 | 1973 | 5 | 5 | ||
| 28 | 14.9 | 66 | 1973 | 5 | 6 | |
| 23 | 299 | 8.6 | 65 | 1973 | 5 | 7 |
| 19 | 99 | 13.8 | 59 | 1973 | 5 | 8 |
| 8 | 19 | 20.1 | 61 | 1973 | 5 | 9 |
| 194 | 8.6 | 69 | 1973 | 5 | 10 | |
gt_airquality = (
GT(airquality_mini)
.tab_header(
title="New York Air Quality Measurements",
subtitle="Daily measurements in New York City (May 1-10, 1973)"
)
.tab_spanner(
label="Time",
columns=["Year", "Month", "Day"]
)
.tab_spanner(
label="Measurement",
columns=["Ozone", "Solar_R", "Wind", "Temp"]
)
)
gt_airquality| New York Air Quality Measurements | ||||||
| Daily measurements in New York City (May 1-10, 1973) | ||||||
| Measurement | Time | |||||
|---|---|---|---|---|---|---|
| Ozone | Solar_R | Wind | Temp | Year | Month | Day |
| 41.0 | 190.0 | 7.4 | 67 | 1973 | 5 | 1 |
| 36.0 | 118.0 | 8.0 | 72 | 1973 | 5 | 2 |
| 12.0 | 149.0 | 12.6 | 74 | 1973 | 5 | 3 |
| 18.0 | 313.0 | 11.5 | 62 | 1973 | 5 | 4 |
| None | None | 14.3 | 56 | 1973 | 5 | 5 |
| 28.0 | None | 14.9 | 66 | 1973 | 5 | 6 |
| 23.0 | 299.0 | 8.6 | 65 | 1973 | 5 | 7 |
| 19.0 | 99.0 | 13.8 | 59 | 1973 | 5 | 8 |
| 8.0 | 19.0 | 20.1 | 61 | 1973 | 5 | 9 |
| None | 194.0 | 8.6 | 69 | 1973 | 5 | 10 |
gt_airquality <- gt(airquality_mini) |>
tab_header(
title = "New York Air Quality Measurements",
subtitle = "Daily measurements in New York City (May 1-10, 1973)"
) |>
tab_spanner(
label = "Time",
columns = c(Year, Month, Day)
) |>
tab_spanner(
label = "Measurement",
columns = c(Ozone, Solar.R, Wind, Temp)
)
gt_airquality| New York Air Quality Measurements | ||||||
| Daily measurements in New York City (May 1-10, 1973) | ||||||
|
Measurement
|
Time
|
|||||
|---|---|---|---|---|---|---|
| Ozone | Solar.R | Wind | Temp | Year | Month | Day |
| 41 | 190 | 7.4 | 67 | 1973 | 5 | 1 |
| 36 | 118 | 8.0 | 72 | 1973 | 5 | 2 |
| 12 | 149 | 12.6 | 74 | 1973 | 5 | 3 |
| 18 | 313 | 11.5 | 62 | 1973 | 5 | 4 |
| NA | NA | 14.3 | 56 | 1973 | 5 | 5 |
| 28 | NA | 14.9 | 66 | 1973 | 5 | 6 |
| 23 | 299 | 8.6 | 65 | 1973 | 5 | 7 |
| 19 | 99 | 13.8 | 59 | 1973 | 5 | 8 |
| 8 | 19 | 20.1 | 61 | 1973 | 5 | 9 |
| NA | 194 | 8.6 | 69 | 1973 | 5 | 10 |
Moving and Relabeling Columns
We can do two more things to make this presentable:
- move the
Timecolumns to the beginning of the series (usingGT.cols_move_to_start()) - customize the column labels so that they are more descriptive (using
GT.cols_label())
Let's do both of these things in the next example:
let relabeled = tabella(gtAirquality):
colsMoveToStart ["Year", "Month", "Day"]
colsLabel:
Ozone: html"Ozone,<br>ppbV"
Solar_R: html"Solar R.,<br>cal/m<sup>2</sup>"
Wind: html"Wind,<br>mph"
Temp: html"Temp,<br>°F"
nbTable relabeled
| New York Air Quality Measurements | ||||||
|---|---|---|---|---|---|---|
| Daily measurements in New York City (May 1-10, 1973) | ||||||
| Time | Measurement | |||||
| Year | Month | Day | Ozone, ppbV | Solar R., cal/m2 | Wind, mph | Temp, °F |
| 1973 | 5 | 1 | 41 | 190 | 7.4 | 67 |
| 1973 | 5 | 2 | 36 | 118 | 8.0 | 72 |
| 1973 | 5 | 3 | 12 | 149 | 12.6 | 74 |
| 1973 | 5 | 4 | 18 | 313 | 11.5 | 62 |
| 1973 | 5 | 5 | 14.3 | 56 | ||
| 1973 | 5 | 6 | 28 | 14.9 | 66 | |
| 1973 | 5 | 7 | 23 | 299 | 8.6 | 65 |
| 1973 | 5 | 8 | 19 | 99 | 13.8 | 59 |
| 1973 | 5 | 9 | 8 | 19 | 20.1 | 61 |
| 1973 | 5 | 10 | 194 | 8.6 | 69 | |
(
gt_airquality
.cols_move_to_start(columns=["Year", "Month", "Day"])
.cols_label(
Ozone=html("Ozone,<br>ppbV"),
Solar_R=html("Solar R.,<br>cal/m<sup>2</sup>"),
Wind=html("Wind,<br>mph"),
Temp=html("Temp,<br>°F")
)
)| New York Air Quality Measurements | ||||||
| Daily measurements in New York City (May 1-10, 1973) | ||||||
| Time | Measurement | |||||
|---|---|---|---|---|---|---|
| Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
| 1973 | 5 | 1 | 41.0 | 190.0 | 7.4 | 67 |
| 1973 | 5 | 2 | 36.0 | 118.0 | 8.0 | 72 |
| 1973 | 5 | 3 | 12.0 | 149.0 | 12.6 | 74 |
| 1973 | 5 | 4 | 18.0 | 313.0 | 11.5 | 62 |
| 1973 | 5 | 5 | None | None | 14.3 | 56 |
| 1973 | 5 | 6 | 28.0 | None | 14.9 | 66 |
| 1973 | 5 | 7 | 23.0 | 299.0 | 8.6 | 65 |
| 1973 | 5 | 8 | 19.0 | 99.0 | 13.8 | 59 |
| 1973 | 5 | 9 | 8.0 | 19.0 | 20.1 | 61 |
| 1973 | 5 | 10 | None | 194.0 | 8.6 | 69 |
gt_airquality |>
cols_move_to_start(columns = c(Year, Month, Day)) |>
cols_label(
Ozone = html("Ozone,<br>ppbV"),
Solar.R = html("Solar R.,<br>cal/m<sup>2</sup>"),
Wind = html("Wind,<br>mph"),
Temp = html("Temp,<br>°F")
)| New York Air Quality Measurements | ||||||
| Daily measurements in New York City (May 1-10, 1973) | ||||||
|
Time
|
Measurement
|
|||||
|---|---|---|---|---|---|---|
| Year | Month | Day | Ozone, ppbV |
Solar R., cal/m2 |
Wind, mph |
Temp, °F |
| 1973 | 5 | 1 | 41 | 190 | 7.4 | 67 |
| 1973 | 5 | 2 | 36 | 118 | 8.0 | 72 |
| 1973 | 5 | 3 | 12 | 149 | 12.6 | 74 |
| 1973 | 5 | 4 | 18 | 313 | 11.5 | 62 |
| 1973 | 5 | 5 | NA | NA | 14.3 | 56 |
| 1973 | 5 | 6 | 28 | NA | 14.9 | 66 |
| 1973 | 5 | 7 | 23 | 299 | 8.6 | 65 |
| 1973 | 5 | 8 | 19 | 99 | 13.8 | 59 |
| 1973 | 5 | 9 | 8 | 19 | 20.1 | 61 |
| 1973 | 5 | 10 | NA | 194 | 8.6 | 69 |
Note that even though columns were moved using GT.cols_move_to_start(), the spanner column labels still spanned above the correct column labels. There are a number of methods on GT to move columns, including GT.cols_move(), GT.cols_move_to_end(); there's even a method to hide columns: GT.cols_hide().
Multiple columns can be renamed in a single use of GT.cols_label(). Further to this, the helper functions md() and html() can be used to create column labels with additional styling. In the above example, we provided column labels as HTML so that we can insert linebreaks with <br>, insert a superscripted 2 (with <sup>2</sup>), and insert a degree symbol as an HTML entity (°).
Targeting Columns for columns=
In the above examples, we selected columns to span or move using a list of column names (as strings). However, Great Tables supports a wide range of ways to select columns.
For example, you can use a lambda function:
let moved = tabella(airqualityMini):
colsMoveToStart endsWith("R")
nbTable moved
| Solar_R | Ozone | Wind | Temp | Month | Day | Year |
|---|---|---|---|---|---|---|
| 190 | 41 | 7.4 | 67 | 5 | 1 | 1973 |
| 118 | 36 | 8.0 | 72 | 5 | 2 | 1973 |
| 149 | 12 | 12.6 | 74 | 5 | 3 | 1973 |
| 313 | 18 | 11.5 | 62 | 5 | 4 | 1973 |
| 14.3 | 56 | 5 | 5 | 1973 | ||
| 28 | 14.9 | 66 | 5 | 6 | 1973 | |
| 299 | 23 | 8.6 | 65 | 5 | 7 | 1973 |
| 99 | 19 | 13.8 | 59 | 5 | 8 | 1973 |
| 19 | 8 | 20.1 | 61 | 5 | 9 | 1973 |
| 194 | 8.6 | 69 | 5 | 10 | 1973 |
(
GT(airquality_mini)
.cols_move_to_start(columns=lambda colname: colname.endswith("R"))
)| Solar_R | Ozone | Wind | Temp | Month | Day | Year |
|---|---|---|---|---|---|---|
| 190.0 | 41.0 | 7.4 | 67 | 5 | 1 | 1973 |
| 118.0 | 36.0 | 8.0 | 72 | 5 | 2 | 1973 |
| 149.0 | 12.0 | 12.6 | 74 | 5 | 3 | 1973 |
| 313.0 | 18.0 | 11.5 | 62 | 5 | 4 | 1973 |
| None | None | 14.3 | 56 | 5 | 5 | 1973 |
| None | 28.0 | 14.9 | 66 | 5 | 6 | 1973 |
| 299.0 | 23.0 | 8.6 | 65 | 5 | 7 | 1973 |
| 99.0 | 19.0 | 13.8 | 59 | 5 | 8 | 1973 |
| 19.0 | 8.0 | 20.1 | 61 | 5 | 9 | 1973 |
| 194.0 | None | 8.6 | 69 | 5 | 10 | 1973 |
gt(airquality_mini) |>
cols_move_to_start(columns = ends_with("R"))| Solar.R | Year | Ozone | Wind | Temp | Month | Day |
|---|---|---|---|---|---|---|
| 190 | 1973 | 41 | 7.4 | 67 | 5 | 1 |
| 118 | 1973 | 36 | 8.0 | 72 | 5 | 2 |
| 149 | 1973 | 12 | 12.6 | 74 | 5 | 3 |
| 313 | 1973 | 18 | 11.5 | 62 | 5 | 4 |
| NA | 1973 | NA | 14.3 | 56 | 5 | 5 |
| NA | 1973 | 28 | 14.9 | 66 | 5 | 6 |
| 299 | 1973 | 23 | 8.6 | 65 | 5 | 7 |
| 99 | 1973 | 19 | 13.8 | 59 | 5 | 8 |
| 19 | 1973 | 8 | 20.1 | 61 | 5 | 9 |
| 194 | 1973 | NA | 8.6 | 69 | 5 | 10 |
Inputs like strings, integers, and polars selectors are also supported. For more information, see Column Selection.
import tabbook
bookInit()
nbText: "# Column Labels"
attribution gtGuide("Column Labels", "basic-column-labels",
"The `Year` column is added to the data in Nim with `addColumn`.")
nbText: """
The table's **Column Labels** part contains, at a minimum, columns and their *column labels*. The last example had a single column: `size`. Just as in the **Stub**, we can create groupings called *spanner labels* that encompass one or more columns.
To better demonstrate how **Column Labels** work and are displayed, let's use an input data table with more columns. In this case, that input table will be `airquality`. It has the following columns:
- `Ozone`: mean ground-level ozone in parts per billion by volume (ppbV), measured between 13:00 and 15:00
- `Solar_R`: solar radiation in Langley units (cal/m<sup>2</sup>), measured between 08:00 and noon
- `Wind`: mean wind speed in miles per hour (mph)
- `Temp`: maximum daily air temperature in degrees Fahrenheit (°F)
- `Month`, `Day`: the numeric month and day of month for the record
We know that all measurements took place in 1973, so a `year` column will be added to the dataset before it is passed to the `GT` class.
"""
tabs """
from great_tables import GT, html
from great_tables.data import airquality
airquality_mini = airquality.head(10).assign(Year = 1973)
airquality_mini
""", """
library(gt)
library(dplyr)
airquality_mini <- airquality |> slice_head(n = 10) |> mutate(Year = 1973L)
airquality_mini
""":
nbCode:
import tabelline, gtdata
import std/sequtils
let airqualityMini = airquality().head(10)
airqualityMini.addColumn("Year", toValues(newSeqWith(10, 1973)))
echo airqualityMini.tabella
tabelline """
Great Tables shows the pandas DataFrame itself here. tabelline's `TableData` has no display of
its own: the closest is a bare table, `tabella(data)`, printed as text with `echo`.
"""
nbText: """
## Adding Column Spanners
Let's organize the time information under a `Time` *spanner label*, and put the other columns under a `Measurement` *spanner label*. We can do this with the `GT.tab_spanner()` method.
"""
tabs """
gt_airquality = (
GT(airquality_mini)
.tab_header(
title="New York Air Quality Measurements",
subtitle="Daily measurements in New York City (May 1-10, 1973)"
)
.tab_spanner(
label="Time",
columns=["Year", "Month", "Day"]
)
.tab_spanner(
label="Measurement",
columns=["Ozone", "Solar_R", "Wind", "Temp"]
)
)
gt_airquality
""", """
gt_airquality <- gt(airquality_mini) |>
tab_header(
title = "New York Air Quality Measurements",
subtitle = "Daily measurements in New York City (May 1-10, 1973)"
) |>
tab_spanner(
label = "Time",
columns = c(Year, Month, Day)
) |>
tab_spanner(
label = "Measurement",
columns = c(Ozone, Solar.R, Wind, Temp)
)
gt_airquality
""":
nbCode:
let gtAirquality = tabella(airqualityMini):
tabHeader "New York Air Quality Measurements",
"Daily measurements in New York City (May 1-10, 1973)"
tabSpanner "Time", ["Year", "Month", "Day"]
tabSpanner "Measurement", ["Ozone", "Solar_R", "Wind", "Temp"]
nbTable gtAirquality
tabelline """
The columns under `Time` are the same, in a different order: Great Tables puts the columns of a
spanner in the order they are given (`Year`, `Month`, `Day`), tabelline leaves them in the
order of the data (`Month`, `Day`, `Year`), which works here because they are already next to
each other. Gathering the columns of a spanner, as Great Tables does, is a gap.
"""
nbText: """
## Moving and Relabeling Columns
We can do two more things to make this presentable:
- move the `Time` columns to the beginning of the series (using `GT.cols_move_to_start()`)
- customize the column labels so that they are more descriptive (using `GT.cols_label()`)
Let's do both of these things in the next example:
"""
tabs """
(
gt_airquality
.cols_move_to_start(columns=["Year", "Month", "Day"])
.cols_label(
Ozone=html("Ozone,<br>ppbV"),
Solar_R=html("Solar R.,<br>cal/m<sup>2</sup>"),
Wind=html("Wind,<br>mph"),
Temp=html("Temp,<br>°F")
)
)
""", """
gt_airquality |>
cols_move_to_start(columns = c(Year, Month, Day)) |>
cols_label(
Ozone = html("Ozone,<br>ppbV"),
Solar.R = html("Solar R.,<br>cal/m<sup>2</sup>"),
Wind = html("Wind,<br>mph"),
Temp = html("Temp,<br>°F")
)
""":
nbCode:
let relabeled = tabella(gtAirquality):
colsMoveToStart ["Year", "Month", "Day"]
colsLabel:
Ozone: html"Ozone,<br>ppbV"
Solar_R: html"Solar R.,<br>cal/m<sup>2</sup>"
Wind: html"Wind,<br>mph"
Temp: html"Temp,<br>°F"
nbTable relabeled
nbText: """
Note that even though columns were moved using `GT.cols_move_to_start()`, the *spanner column labels* still spanned above the correct *column labels*. There are a number of methods on `GT` to move columns, including `GT.cols_move()`, `GT.cols_move_to_end()`; there's even a method to hide columns: `GT.cols_hide()`.
Multiple columns can be renamed in a single use of `GT.cols_label()`. Further to this, the helper functions `md()` and `html()` can be used to create column labels with additional styling. In the above example, we provided column labels as HTML so that we can insert linebreaks with `<br>`, insert a superscripted `2` (with `<sup>2</sup>`), and insert a degree symbol as an HTML entity (`°`).
"""
tabelline """
tabelline has `colsMove`, `colsMoveToStart`, `colsMoveToEnd`, `colsReorder`, `colsHide` and
`colsUnhide`. Inside `tabella(data): body`, `colsLabel` takes its pairs as a block, one
`column: label` per line, where a label is a string, `md"..."` or `html"..."` (outside a body,
the same pairs go in a table constructor, `colsLabel({"Ozone": ...})`). With `md`, the same
labels need no HTML: `md"Solar R.,<br>cal/m^2^"`; and `colsUnits` writes units in gt's
notation, so `Solar_R: "cal/m^2"` in a `colsUnits:` block shows "Solar_R, cal/m²".
"""
nbText: """
## Targeting Columns for `columns=`
In the above examples, we selected columns to span or move using a list of column names (as strings). However, **Great Tables** supports a wide range of ways to select columns.
For example, you can use a lambda function:
"""
tabs """
(
GT(airquality_mini)
.cols_move_to_start(columns=lambda colname: colname.endswith("R"))
)
""", """
gt(airquality_mini) |>
cols_move_to_start(columns = ends_with("R"))
""":
nbCode:
let moved = tabella(airqualityMini):
colsMoveToStart endsWith("R")
nbTable moved
nbText: """
Inputs like strings, integers, and polars selectors are also supported. For more information, see [Column Selection](column_selection.html).
"""
tabelline """
tabelline selects columns with rules instead of functions: `everything()`, `startsWith`,
`endsWith`, `containing`, `numeric()` and `textual()`, or names. Selecting by position, and
with any function of the name, are gaps (see [Column Selection](column_selection.html)).
"""
bookSave()
On GitHub: book/get-started/basic_column_labels.nim