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:00
  • Solar_R: solar radiation in Langley units (cal/m2), 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.

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 │
└───────┴─────────┴──────┴──────┴───────┴─────┴──────┘

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)
MeasurementTime
OzoneSolar_RWindTempYearMonthDay
411907.467197351
361188.072197352
1214912.674197353
1831311.562197354
14.356197355
2814.966197356
232998.665197357
199913.859197358
81920.161197359
1948.6691973510

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:

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>&deg;F"
nbTable relabeled
New York Air Quality Measurements
Daily measurements in New York City (May 1-10, 1973)
TimeMeasurement
YearMonthDayOzone,
ppbV
Solar R.,
cal/m2
Wind,
mph
Temp,
°F
197351411907.467
197352361188.072
1973531214912.674
1973541831311.562
19735514.356
1973562814.966
197357232998.665
197358199913.859
19735981920.161
19735101948.669

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 (&deg;).

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_ROzoneWindTempMonthDayYear
190417.467511973
118368.072521973
1491212.674531973
3131811.562541973
14.356551973
2814.966561973
299238.665571973
991913.859581973
19820.161591973
1948.6695101973

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 (&deg;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>&deg;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>&deg;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>&deg;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 (`&deg;`).
"""

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()
Made with tabelline, nimib and nimibook. The Nim code ran when this page was built; the Python ran with Great Tables 1.1.0 and the R with gt 1.4.0.