Skip to main content

loc.summary

Target the data cells in group summary rows.

Usage

Source

loc.summary(
    groups=None,
    columns=None,
    rows=None,
)

With loc.summary() we can target the cells containing the group summary data. This is useful for applying custom styling with the tab_style() method. That method has a locations= argument and this class should be used there to perform the targeting.

Parameters

groups: list[str] | str | None = None

The groups to target. Can be a list of group IDs or a single group ID string. If None, all groups with summary rows are targeted.

columns: SelectExpr = None

The columns to target. Can either be a single column name or a series of column names provided in a list.

rows: RowLiteralSelectExpr = None
The rows to target. Can either be a single row name or a series of row names provided in a list.

Returns

LocSummary
A LocSummary object, which is used for a locations= argument if specifying the table’s group summary rows.

Examples

Let’s use a subset of the gtcars dataset in a new table. We will style all of the group summary cells by using locations=loc.summary() within tab_style().

import polars as pl
from great_tables import GT, data, style, loc, vals

gtcars_mini = (
    data.pl.gtcars
    .select(["mfr", "model", "hp", "trq"])
    .head(12)
)

(
    GT(gtcars_mini, rowname_col="model", groupname_col="mfr")
    .summary_rows(
        fns={
            "Min": pl.col("hp", "trq").min(),
            "Max": pl.col("hp", "trq").max(),
        },
        fmt=vals.fmt_integer,
    )
    .tab_style(
        style=[style.text(color="crimson"), style.fill(color="lightgray")],
        locations=loc.summary(),
    )
)
hp trq
Ford
GT 647.0 550.0
Min 647 550
Max 647 550
Ferrari
458 Speciale 597.0 398.0
458 Spider 562.0 398.0
458 Italia 562.0 398.0
488 GTB 661.0 561.0
California 553.0 557.0
GTC4Lusso 680.0 514.0
FF 652.0 504.0
F12Berlinetta 731.0 509.0
LaFerrari 949.0 664.0
Min 553 398
Max 949 664
Acura
NSX 573.0 476.0
Min 573 476
Max 573 476
Nissan
GT-R 545.0 436.0
Min 545 436
Max 545 436