Loading gtcars

Loading gtcars

from itertools import count

import polars as pl
from great_tables import GT, html, loc, md, style
from great_tables._helpers import px
from great_tables.data import gtcars as gtcars_pd

# pl.Config.set_tbl_rows(100)
gtcars = pl.from_pandas(gtcars_pd)
gtcars.head()
shape: (5, 15)
mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn ctry_origin msrp
str str f64 str str f64 f64 f64 f64 f64 f64 str str str f64
"Ford" "GT" 2017.0 "Base Coupe" "coupe" 647.0 6250.0 550.0 5900.0 11.0 18.0 "rwd" "7a" "United States" 447000.0
"Ferrari" "458 Speciale" 2015.0 "Base Coupe" "coupe" 597.0 9000.0 398.0 6000.0 13.0 17.0 "rwd" "7a" "Italy" 291744.0
"Ferrari" "458 Spider" 2015.0 "Base" "convertible" 562.0 9000.0 398.0 6000.0 13.0 17.0 "rwd" "7a" "Italy" 263553.0
"Ferrari" "458 Italia" 2014.0 "Base Coupe" "coupe" 562.0 9000.0 398.0 6000.0 13.0 17.0 "rwd" "7a" "Italy" 233509.0
"Ferrari" "488 GTB" 2016.0 "Base Coupe" "coupe" 661.0 8000.0 561.0 3000.0 15.0 22.0 "rwd" "7a" "Italy" 245400.0
cast_dict = {
    "year": pl.UInt16,
    "hp": pl.UInt32,
    "hp_rpm": pl.UInt32,
    "trq": pl.UInt32,
    "trq_rpm": pl.UInt32,
    "mpg_c": pl.UInt8,
    "mpg_h": pl.UInt8,
    "msrp": pl.UInt32,
}

gtcars = gtcars.cast(cast_dict)
gtcars.head()
shape: (5, 15)
mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn ctry_origin msrp
str str u16 str str u32 u32 u32 u32 u8 u8 str str str u32
"Ford" "GT" 2017 "Base Coupe" "coupe" 647 6250 550 5900 11 18 "rwd" "7a" "United States" 447000
"Ferrari" "458 Speciale" 2015 "Base Coupe" "coupe" 597 9000 398 6000 13 17 "rwd" "7a" "Italy" 291744
"Ferrari" "458 Spider" 2015 "Base" "convertible" 562 9000 398 6000 13 17 "rwd" "7a" "Italy" 263553
"Ferrari" "458 Italia" 2014 "Base Coupe" "coupe" 562 9000 398 6000 13 17 "rwd" "7a" "Italy" 233509
"Ferrari" "488 GTB" 2016 "Base Coupe" "coupe" 661 8000 561 3000 15 22 "rwd" "7a" "Italy" 245400

Row groups

Get 2 rows from each group

gtcars_8 = (
    gtcars.sort("ctry_origin")
    .select(pl.col("ctry_origin"), pl.all().exclude("ctry_origin"))
    .filter(pl.col("ctry_origin").ne("United Kingdom"))
    .filter(pl.int_range(pl.len()).over("ctry_origin").lt(2))
)
gtcars_8
shape: (8, 15)
ctry_origin mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp
str str str u16 str str u32 u32 u32 u32 u8 u8 str str u32
"Germany" "BMW" "6-Series" 2016 "640 I Coupe" "coupe" 315 5800 330 1400 20 30 "rwd" "8am" 77300
"Germany" "BMW" "i8" 2016 "Mega World Coupe" "coupe" 357 5800 420 3700 28 29 "awd" "6am" 140700
"Italy" "Ferrari" "458 Speciale" 2015 "Base Coupe" "coupe" 597 9000 398 6000 13 17 "rwd" "7a" 291744
"Italy" "Ferrari" "458 Spider" 2015 "Base" "convertible" 562 9000 398 6000 13 17 "rwd" "7a" 263553
"Japan" "Acura" "NSX" 2017 "Base Coupe" "coupe" 573 6500 476 2000 21 22 "awd" "9a" 156000
"Japan" "Nissan" "GT-R" 2016 "Premium Coupe" "coupe" 545 6400 436 3200 16 22 "awd" "6a" 101770
"United States" "Ford" "GT" 2017 "Base Coupe" "coupe" 647 6250 550 5900 11 18 "rwd" "7a" 447000
"United States" "Chevrolet" "Corvette" 2016 "Z06 Coupe" "coupe" 650 6400 650 3600 15 22 "rwd" "7m" 88345

Sort

gtcars_8_sort_by = (
    gtcars.sort("ctry_origin")
    .select(pl.col("ctry_origin"), pl.all().exclude("ctry_origin"))
    .filter(pl.col("ctry_origin").ne("United Kingdom"))
    .filter(pl.int_range(pl.len()).over("ctry_origin").lt(2))
    .select(
        pl.all().sort_by(["mfr", "msrp"], descending=[False, True]).over("ctry_origin")
    )
)
gtcars_8_sort_by
shape: (8, 15)
ctry_origin mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp
str str str u16 str str u32 u32 u32 u32 u8 u8 str str u32
"Germany" "BMW" "i8" 2016 "Mega World Coupe" "coupe" 357 5800 420 3700 28 29 "awd" "6am" 140700
"Germany" "BMW" "6-Series" 2016 "640 I Coupe" "coupe" 315 5800 330 1400 20 30 "rwd" "8am" 77300
"Italy" "Ferrari" "458 Speciale" 2015 "Base Coupe" "coupe" 597 9000 398 6000 13 17 "rwd" "7a" 291744
"Italy" "Ferrari" "458 Spider" 2015 "Base" "convertible" 562 9000 398 6000 13 17 "rwd" "7a" 263553
"Japan" "Acura" "NSX" 2017 "Base Coupe" "coupe" 573 6500 476 2000 21 22 "awd" "9a" 156000
"Japan" "Nissan" "GT-R" 2016 "Premium Coupe" "coupe" 545 6400 436 3200 16 22 "awd" "6a" 101770
"United States" "Chevrolet" "Corvette" 2016 "Z06 Coupe" "coupe" 650 6400 650 3600 15 22 "rwd" "7m" 88345
"United States" "Ford" "GT" 2017 "Base Coupe" "coupe" 647 6250 550 5900 11 18 "rwd" "7a" 447000

Preferred group order

order_countries = dict(
    zip(("Germany", "Italy", "United States", "Japan", "United Kingdom"), count())
)
order_countries  # {'Germany': 0, 'Italy': 1, 'United States': 2, 'Japan': 3, 'United Kingdom': 4}

gtcars_8_pref_group_order = (
    gtcars.sort(pl.col("ctry_origin").replace(order_countries, return_dtype=pl.UInt8))
    .select(pl.col("ctry_origin"), pl.all().exclude("ctry_origin"))
    .filter(pl.col("ctry_origin").ne("United Kingdom"))
    .filter(pl.int_range(pl.len()).over("ctry_origin").lt(2))
    .select(
        pl.all().sort_by(["mfr", "msrp"], descending=[False, True]).over("ctry_origin")
    )
)
gtcars_8_pref_group_order
shape: (8, 15)
ctry_origin mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp
str str str u16 str str u32 u32 u32 u32 u8 u8 str str u32
"Germany" "BMW" "i8" 2016 "Mega World Coupe" "coupe" 357 5800 420 3700 28 29 "awd" "6am" 140700
"Germany" "BMW" "6-Series" 2016 "640 I Coupe" "coupe" 315 5800 330 1400 20 30 "rwd" "8am" 77300
"Italy" "Ferrari" "458 Speciale" 2015 "Base Coupe" "coupe" 597 9000 398 6000 13 17 "rwd" "7a" 291744
"Italy" "Ferrari" "458 Spider" 2015 "Base" "convertible" 562 9000 398 6000 13 17 "rwd" "7a" 263553
"United States" "Chevrolet" "Corvette" 2016 "Z06 Coupe" "coupe" 650 6400 650 3600 15 22 "rwd" "7m" 88345
"United States" "Ford" "GT" 2017 "Base Coupe" "coupe" 647 6250 550 5900 11 18 "rwd" "7a" 447000
"Japan" "Acura" "NSX" 2017 "Base Coupe" "coupe" 573 6500 476 2000 21 22 "awd" "9a" 156000
"Japan" "Nissan" "GT-R" 2016 "Premium Coupe" "coupe" 545 6400 436 3200 16 22 "awd" "6a" 101770
GT(
    gtcars_8_pref_group_order, groupname_col="ctry_origin", rowname_col="mfr"
).tab_stubhead(label="mfr")
mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp
Germany
BMW i8 2016 Mega World Coupe coupe 357 5800 420 3700 28 29 awd 6am 140700
BMW 6-Series 2016 640 I Coupe coupe 315 5800 330 1400 20 30 rwd 8am 77300
Italy
Ferrari 458 Speciale 2015 Base Coupe coupe 597 9000 398 6000 13 17 rwd 7a 291744
Ferrari 458 Spider 2015 Base convertible 562 9000 398 6000 13 17 rwd 7a 263553
United States
Chevrolet Corvette 2016 Z06 Coupe coupe 650 6400 650 3600 15 22 rwd 7m 88345
Ford GT 2017 Base Coupe coupe 647 6250 550 5900 11 18 rwd 7a 447000
Japan
Acura NSX 2017 Base Coupe coupe 573 6500 476 2000 21 22 awd 9a 156000
Nissan GT-R 2016 Premium Coupe coupe 545 6400 436 3200 16 22 awd 6a 101770

Combined label

gtcars_8_combined_label = gtcars_8_pref_group_order.with_columns(
    c_label=pl.col("mfr").add(pl.lit(" ")).add(pl.col("model"))
)
gtcars_8_combined_label
shape: (8, 16)
ctry_origin mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp c_label
str str str u16 str str u32 u32 u32 u32 u8 u8 str str u32 str
"Germany" "BMW" "i8" 2016 "Mega World Coupe" "coupe" 357 5800 420 3700 28 29 "awd" "6am" 140700 "BMW i8"
"Germany" "BMW" "6-Series" 2016 "640 I Coupe" "coupe" 315 5800 330 1400 20 30 "rwd" "8am" 77300 "BMW 6-Series"
"Italy" "Ferrari" "458 Speciale" 2015 "Base Coupe" "coupe" 597 9000 398 6000 13 17 "rwd" "7a" 291744 "Ferrari 458 Speciale"
"Italy" "Ferrari" "458 Spider" 2015 "Base" "convertible" 562 9000 398 6000 13 17 "rwd" "7a" 263553 "Ferrari 458 Spider"
"United States" "Chevrolet" "Corvette" 2016 "Z06 Coupe" "coupe" 650 6400 650 3600 15 22 "rwd" "7m" 88345 "Chevrolet Corvette"
"United States" "Ford" "GT" 2017 "Base Coupe" "coupe" 647 6250 550 5900 11 18 "rwd" "7a" 447000 "Ford GT"
"Japan" "Acura" "NSX" 2017 "Base Coupe" "coupe" 573 6500 476 2000 21 22 "awd" "9a" 156000 "Acura NSX"
"Japan" "Nissan" "GT-R" 2016 "Premium Coupe" "coupe" 545 6400 436 3200 16 22 "awd" "6a" 101770 "Nissan GT-R"
(
    GT(
        gtcars_8_combined_label, groupname_col="ctry_origin", rowname_col="c_label"
    ).cols_hide(["mfr", "model"])
)
year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp
Germany
BMW i8 2016 Mega World Coupe coupe 357 5800 420 3700 28 29 awd 6am 140700
BMW 6-Series 2016 640 I Coupe coupe 315 5800 330 1400 20 30 rwd 8am 77300
Italy
Ferrari 458 Speciale 2015 Base Coupe coupe 597 9000 398 6000 13 17 rwd 7a 291744
Ferrari 458 Spider 2015 Base convertible 562 9000 398 6000 13 17 rwd 7a 263553
United States
Chevrolet Corvette 2016 Z06 Coupe coupe 650 6400 650 3600 15 22 rwd 7m 88345
Ford GT 2017 Base Coupe coupe 647 6250 550 5900 11 18 rwd 7a 447000
Japan
Acura NSX 2017 Base Coupe coupe 573 6500 476 2000 21 22 awd 9a 156000
Nissan GT-R 2016 Premium Coupe coupe 545 6400 436 3200 16 22 awd 6a 101770

Hiding and Moving Some Columns

(
    GT(gtcars_8_combined_label, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(["mfr", "model", "drivetrain", "bdy_style"])
    .cols_move(["trsmn", "mpg_c", "mpg_h"], after="trim")
)
year trim trsmn mpg_c mpg_h hp hp_rpm trq trq_rpm msrp
Germany
BMW i8 2016 Mega World Coupe 6am 28 29 357 5800 420 3700 140700
BMW 6-Series 2016 640 I Coupe 8am 20 30 315 5800 330 1400 77300
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13 17 597 9000 398 6000 291744
Ferrari 458 Spider 2015 Base 7a 13 17 562 9000 398 6000 263553
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15 22 650 6400 650 3600 88345
Ford GT 2017 Base Coupe 7a 11 18 647 6250 550 5900 447000
Japan
Acura NSX 2017 Base Coupe 9a 21 22 573 6500 476 2000 156000
Nissan GT-R 2016 Premium Coupe 6a 16 22 545 6400 436 3200 101770

Putting Columns Into Groups

(
    GT(gtcars_8_combined_label, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(["mfr", "model", "drivetrain", "bdy_style"])
    .cols_move(["trsmn", "mpg_c", "mpg_h"], after="trim")
    .tab_spanner(
        label="Performance",
        columns=["mpg_c", "mpg_h", "hp", "hp_rpm", "trq", "trq_rpm"],
    )
)
year trim trsmn Performance msrp
mpg_c mpg_h hp hp_rpm trq trq_rpm
Germany
BMW i8 2016 Mega World Coupe 6am 28 29 357 5800 420 3700 140700
BMW 6-Series 2016 640 I Coupe 8am 20 30 315 5800 330 1400 77300
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13 17 597 9000 398 6000 291744
Ferrari 458 Spider 2015 Base 7a 13 17 562 9000 398 6000 263553
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15 22 650 6400 650 3600 88345
Ford GT 2017 Base Coupe 7a 11 18 647 6250 550 5900 447000
Japan
Acura NSX 2017 Base Coupe 9a 21 22 573 6500 476 2000 156000
Nissan GT-R 2016 Premium Coupe 6a 16 22 545 6400 436 3200 101770

Merging Columns Together and Labeling Them

def fmt_performance(col1: str, c1: str, col2: str, c2: str) -> pl.Expr:
    return (
        pl.col(col1)
        .cast(pl.Utf8)
        .add(pl.lit(c1))
        .add(pl.col(col2).cast(pl.Utf8).add(pl.lit(c2)))
    )


gtcars_8_merge_cols = gtcars_8_combined_label.with_columns(
    mpg_c=fmt_performance("mpg_c", "c<br>", "mpg_h", "h"),
    hp=fmt_performance("hp", "<br>@", "hp_rpm", "rpm"),
    trq=fmt_performance("trq", "<br>@", "trq_rpm", "rpm"),
)
gtcars_8_merge_cols
shape: (8, 16)
ctry_origin mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp c_label
str str str u16 str str str u32 str u32 str u8 str str u32 str
"Germany" "BMW" "i8" 2016 "Mega World Coupe" "coupe" "357<br>@5800rpm" 5800 "420<br>@3700rpm" 3700 "28c<br>29h" 29 "awd" "6am" 140700 "BMW i8"
"Germany" "BMW" "6-Series" 2016 "640 I Coupe" "coupe" "315<br>@5800rpm" 5800 "330<br>@1400rpm" 1400 "20c<br>30h" 30 "rwd" "8am" 77300 "BMW 6-Series"
"Italy" "Ferrari" "458 Speciale" 2015 "Base Coupe" "coupe" "597<br>@9000rpm" 9000 "398<br>@6000rpm" 6000 "13c<br>17h" 17 "rwd" "7a" 291744 "Ferrari 458 Speciale"
"Italy" "Ferrari" "458 Spider" 2015 "Base" "convertible" "562<br>@9000rpm" 9000 "398<br>@6000rpm" 6000 "13c<br>17h" 17 "rwd" "7a" 263553 "Ferrari 458 Spider"
"United States" "Chevrolet" "Corvette" 2016 "Z06 Coupe" "coupe" "650<br>@6400rpm" 6400 "650<br>@3600rpm" 3600 "15c<br>22h" 22 "rwd" "7m" 88345 "Chevrolet Corvette"
"United States" "Ford" "GT" 2017 "Base Coupe" "coupe" "647<br>@6250rpm" 6250 "550<br>@5900rpm" 5900 "11c<br>18h" 18 "rwd" "7a" 447000 "Ford GT"
"Japan" "Acura" "NSX" 2017 "Base Coupe" "coupe" "573<br>@6500rpm" 6500 "476<br>@2000rpm" 2000 "21c<br>22h" 22 "awd" "9a" 156000 "Acura NSX"
"Japan" "Nissan" "GT-R" 2016 "Premium Coupe" "coupe" "545<br>@6400rpm" 6400 "436<br>@3200rpm" 3200 "16c<br>22h" 22 "awd" "6a" 101770 "Nissan GT-R"
col_labels = {
    "mpg_c": "MPG",
    "hp": "HP",
    "trq": "Torque",
    "year": "Year",
    "trim": "Trim",
    "trsmn": "Transmission",
    "msrp": "MSRP",
}

(
    GT(gtcars_8_merge_cols, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(
        ["mfr", "model", "drivetrain", "bdy_style", "mpg_h", "hp_rpm", "trq_rpm"]
    )
    .cols_move(["trsmn", "mpg_c"], after="trim")
    .tab_spanner(label="Performance", columns=["mpg_c", "hp", "trq"])
    .cols_label(**col_labels)
)
Year Trim Transmission Performance MSRP
MPG HP Torque
Germany
BMW i8 2016 Mega World Coupe 6am 28c
29h
357
@5800rpm
420
@3700rpm
140700
BMW 6-Series 2016 640 I Coupe 8am 20c
30h
315
@5800rpm
330
@1400rpm
77300
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13c
17h
597
@9000rpm
398
@6000rpm
291744
Ferrari 458 Spider 2015 Base 7a 13c
17h
562
@9000rpm
398
@6000rpm
263553
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15c
22h
650
@6400rpm
650
@3600rpm
88345
Ford GT 2017 Base Coupe 7a 11c
18h
647
@6250rpm
550
@5900rpm
447000
Japan
Acura NSX 2017 Base Coupe 9a 21c
22h
573
@6500rpm
476
@2000rpm
156000
Nissan GT-R 2016 Premium Coupe 6a 16c
22h
545
@6400rpm
436
@3200rpm
101770

Using Formatter Functions

(
    GT(gtcars_8_merge_cols, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(
        ["mfr", "model", "drivetrain", "bdy_style", "mpg_h", "hp_rpm", "trq_rpm"]
    )
    .cols_move(["trsmn", "mpg_c"], after="trim")
    .tab_spanner(label="Performance", columns=["mpg_c", "hp", "trq"])
    .cols_label(**col_labels)
    .fmt_currency("msrp", decimals=0)
)
Year Trim Transmission Performance MSRP
MPG HP Torque
Germany
BMW i8 2016 Mega World Coupe 6am 28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8am 20c
30h
315
@5800rpm
330
@1400rpm
$77,300
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7a 13c
17h
562
@9000rpm
398
@6000rpm
$263,553
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7a 11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Japan
Acura NSX 2017 Base Coupe 9a 21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6a 16c
22h
545
@6400rpm
436
@3200rpm
$101,770

Column Alignment and Style Changes

(
    GT(gtcars_8_merge_cols, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(
        ["mfr", "model", "drivetrain", "bdy_style", "mpg_h", "hp_rpm", "trq_rpm"]
    )
    .cols_move(["trsmn", "mpg_c"], after="trim")
    .tab_spanner(label="Performance", columns=["mpg_c", "hp", "trq"])
    .cols_label(**col_labels)
    .fmt_currency("msrp", decimals=0)
    .cols_align(align="center", columns=["mpg_c", "hp", "trq"])
    .tab_style(
        style=style.text(size=px("12")),
        locations=loc.body(columns=["trim", "trsmn", "mpg_c", "hp", "trq"]),
    )
)
Year Trim Transmission Performance MSRP
MPG HP Torque
Germany
BMW i8 2016 Mega World Coupe 6am 28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8am 20c
30h
315
@5800rpm
330
@1400rpm
$77,300
Italy
Ferrari 458 Speciale 2015 Base Coupe 7a 13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7a 13c
17h
562
@9000rpm
398
@6000rpm
$263,553
United States
Chevrolet Corvette 2016 Z06 Coupe 7m 15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7a 11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Japan
Acura NSX 2017 Base Coupe 9a 21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6a 16c
22h
545
@6400rpm
436
@3200rpm
$101,770

Text Transforms

gtcars_8_text_transform = (
    gtcars_8_merge_cols.with_columns(
        speed=pl.col("trsmn").str.slice(0, 1).cast(pl.UInt8),
        type_=pl.col("trsmn").str.slice(1),
    )
    .with_columns(
        type_=pl.when(pl.col("type_").eq("am"))
        .then(pl.lit("Automatic/Manual"))
        .when(pl.col("type_").eq("m"))
        .then(pl.lit("Manual"))
        .when(pl.col("type_").eq("a"))
        .then(pl.lit("Automatic"))
        .otherwise(pl.lit("Direct Drive"))
    )
    .with_columns(
        trsmn=pl.col("speed")
        .cast(pl.Utf8)
        .add(pl.lit(" Speed<br><em>").add(pl.col("type_")))
        .add(pl.lit("</em>"))
    )
    .select(pl.all().exclude(["speed", "type_"]))
)
gtcars_8_text_transform
shape: (8, 16)
ctry_origin mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp c_label
str str str u16 str str str u32 str u32 str u8 str str u32 str
"Germany" "BMW" "i8" 2016 "Mega World Coupe" "coupe" "357<br>@5800rpm" 5800 "420<br>@3700rpm" 3700 "28c<br>29h" 29 "awd" "6 Speed<br><em>Automatic/Manua… 140700 "BMW i8"
"Germany" "BMW" "6-Series" 2016 "640 I Coupe" "coupe" "315<br>@5800rpm" 5800 "330<br>@1400rpm" 1400 "20c<br>30h" 30 "rwd" "8 Speed<br><em>Automatic/Manua… 77300 "BMW 6-Series"
"Italy" "Ferrari" "458 Speciale" 2015 "Base Coupe" "coupe" "597<br>@9000rpm" 9000 "398<br>@6000rpm" 6000 "13c<br>17h" 17 "rwd" "7 Speed<br><em>Automatic</em>" 291744 "Ferrari 458 Speciale"
"Italy" "Ferrari" "458 Spider" 2015 "Base" "convertible" "562<br>@9000rpm" 9000 "398<br>@6000rpm" 6000 "13c<br>17h" 17 "rwd" "7 Speed<br><em>Automatic</em>" 263553 "Ferrari 458 Spider"
"United States" "Chevrolet" "Corvette" 2016 "Z06 Coupe" "coupe" "650<br>@6400rpm" 6400 "650<br>@3600rpm" 3600 "15c<br>22h" 22 "rwd" "7 Speed<br><em>Manual</em>" 88345 "Chevrolet Corvette"
"United States" "Ford" "GT" 2017 "Base Coupe" "coupe" "647<br>@6250rpm" 6250 "550<br>@5900rpm" 5900 "11c<br>18h" 18 "rwd" "7 Speed<br><em>Automatic</em>" 447000 "Ford GT"
"Japan" "Acura" "NSX" 2017 "Base Coupe" "coupe" "573<br>@6500rpm" 6500 "476<br>@2000rpm" 2000 "21c<br>22h" 22 "awd" "9 Speed<br><em>Automatic</em>" 156000 "Acura NSX"
"Japan" "Nissan" "GT-R" 2016 "Premium Coupe" "coupe" "545<br>@6400rpm" 6400 "436<br>@3200rpm" 3200 "16c<br>22h" 22 "awd" "6 Speed<br><em>Automatic</em>" 101770 "Nissan GT-R"
(
    GT(gtcars_8_text_transform, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(
        ["mfr", "model", "drivetrain", "bdy_style", "mpg_h", "hp_rpm", "trq_rpm"]
    )
    .cols_move(["trsmn", "mpg_c"], after="trim")
    .tab_spanner(label="Performance", columns=["mpg_c", "hp", "trq"])
    .cols_label(**col_labels)
    .fmt_currency("msrp", decimals=0)
    .cols_align(align="center", columns=["mpg_c", "hp", "trq"])
    .tab_style(
        style=style.text(size=px("12")),
        locations=loc.body(columns=["trim", "trsmn", "mpg_c", "hp", "trq"]),
    )
)
Year Trim Transmission Performance MSRP
MPG HP Torque
Germany
BMW i8 2016 Mega World Coupe 6 Speed
Automatic/Manual
28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8 Speed
Automatic/Manual
20c
30h
315
@5800rpm
330
@1400rpm
$77,300
Italy
Ferrari 458 Speciale 2015 Base Coupe 7 Speed
Automatic
13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7 Speed
Automatic
13c
17h
562
@9000rpm
398
@6000rpm
$263,553
United States
Chevrolet Corvette 2016 Z06 Coupe 7 Speed
Manual
15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7 Speed
Automatic
11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Japan
Acura NSX 2017 Base Coupe 9 Speed
Automatic
21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6 Speed
Automatic
16c
22h
545
@6400rpm
436
@3200rpm
$101,770

Table Header: Title and Subtitle

(
    GT(gtcars_8_text_transform, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(
        ["mfr", "model", "drivetrain", "bdy_style", "mpg_h", "hp_rpm", "trq_rpm"]
    )
    .cols_move(["trsmn", "mpg_c"], after="trim")
    .tab_spanner(label="Performance", columns=["mpg_c", "hp", "trq"])
    .cols_label(**col_labels)
    .fmt_currency("msrp", decimals=0)
    .cols_align(align="center", columns=["mpg_c", "hp", "trq"])
    .tab_style(
        style=style.text(size=px("12")),
        locations=loc.body(columns=["trim", "trsmn", "mpg_c", "hp", "trq"]),
    )
    .tab_header(
        title=md("The Cars of **gtcars**"), subtitle="These are some fine automobiles"
    )
)
The Cars of gtcars
These are some fine automobiles
Year Trim Transmission Performance MSRP
MPG HP Torque
Germany
BMW i8 2016 Mega World Coupe 6 Speed
Automatic/Manual
28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8 Speed
Automatic/Manual
20c
30h
315
@5800rpm
330
@1400rpm
$77,300
Italy
Ferrari 458 Speciale 2015 Base Coupe 7 Speed
Automatic
13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7 Speed
Automatic
13c
17h
562
@9000rpm
398
@6000rpm
$263,553
United States
Chevrolet Corvette 2016 Z06 Coupe 7 Speed
Manual
15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7 Speed
Automatic
11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Japan
Acura NSX 2017 Base Coupe 9 Speed
Automatic
21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6 Speed
Automatic
16c
22h
545
@6400rpm
436
@3200rpm
$101,770

Adding a Source Citation

(
    GT(gtcars_8_text_transform, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(
        ["mfr", "model", "drivetrain", "bdy_style", "mpg_h", "hp_rpm", "trq_rpm"]
    )
    .cols_move(["trsmn", "mpg_c"], after="trim")
    .tab_spanner(label="Performance", columns=["mpg_c", "hp", "trq"])
    .cols_label(**col_labels)
    .fmt_currency("msrp", decimals=0)
    .cols_align(align="center", columns=["mpg_c", "hp", "trq"])
    .tab_style(
        style=style.text(size=px("12")),
        locations=loc.body(columns=["trim", "trsmn", "mpg_c", "hp", "trq"]),
    )
    .tab_header(
        title=md("The Cars of **gtcars**"), subtitle="These are some fine automobiles"
    )
    .tab_source_note(md("Source: Various pages within the Edmonds website."))
)
The Cars of gtcars
These are some fine automobiles
Year Trim Transmission Performance MSRP
MPG HP Torque
Germany
BMW i8 2016 Mega World Coupe 6 Speed
Automatic/Manual
28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW 6-Series 2016 640 I Coupe 8 Speed
Automatic/Manual
20c
30h
315
@5800rpm
330
@1400rpm
$77,300
Italy
Ferrari 458 Speciale 2015 Base Coupe 7 Speed
Automatic
13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7 Speed
Automatic
13c
17h
562
@9000rpm
398
@6000rpm
$263,553
United States
Chevrolet Corvette 2016 Z06 Coupe 7 Speed
Manual
15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Ford GT 2017 Base Coupe 7 Speed
Automatic
11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Japan
Acura NSX 2017 Base Coupe 9 Speed
Automatic
21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6 Speed
Automatic
16c
22h
545
@6400rpm
436
@3200rpm
$101,770
Source: Various pages within the Edmonds website.

Using the Complete gtcars table

gtcars_final = (
    gtcars.sort(pl.col("ctry_origin").replace(order_countries, return_dtype=pl.UInt8))
    .select(pl.col("ctry_origin"), pl.all().exclude("ctry_origin"))
    .select(
        pl.all().sort_by(["mfr", "msrp"], descending=[False, True]).over("ctry_origin")
    )
    .with_columns(c_label=pl.col("mfr").add(pl.lit(" ")).add(pl.col("model")))
    .with_columns(
        mpg_c=fmt_performance("mpg_c", "c<br>", "mpg_h", "h"),
        hp=fmt_performance("hp", "<br>@", "hp_rpm", "rpm"),
        trq=fmt_performance("trq", "<br>@", "trq_rpm", "rpm"),
    )
    .with_columns(
        speed=pl.col("trsmn").str.slice(0, 1).cast(pl.UInt8),
        type_=pl.col("trsmn").str.slice(1),
    )
    .with_columns(
        type_=pl.when(pl.col("type_").eq("am"))
        .then(pl.lit("Automatic/Manual"))
        .when(pl.col("type_").eq("m"))
        .then(pl.lit("Manual"))
        .when(pl.col("type_").eq("a"))
        .then(pl.lit("Automatic"))
        .otherwise(pl.lit("Direct Drive"))
    )
    .with_columns(
        trsmn=pl.col("speed")
        .cast(pl.Utf8)
        .add(pl.lit(" Speed<br><em>").add(pl.col("type_")))
        .add(pl.lit("</em>"))
    )
    .select(pl.all().exclude(["speed", "type_"]))
)
gtcars_final.head()
shape: (5, 16)
ctry_origin mfr model year trim bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h drivetrain trsmn msrp c_label
str str str u16 str str str u32 str u32 str u8 str str u32 str
"Germany" "Audi" "R8" 2015 "4.2 (Manual) Coupe" "coupe" "430<br>@7900rpm" 7900 "317<br>@4500rpm" 4500 "11c<br>20h" 20 "awd" "6 Speed<br><em>Manual</em>" 115900 "Audi R8"
"Germany" "Audi" "S8" 2016 "Base Sedan" "sedan" "520<br>@5800rpm" 5800 "481<br>@1700rpm" 1700 "15c<br>25h" 25 "awd" "8 Speed<br><em>Automatic/Manua… 114900 "Audi S8"
"Germany" "Audi" "RS 7" 2016 "Quattro Hatchback" "hatchback" "560<br>@5700rpm" 5700 "516<br>@1750rpm" 1750 "15c<br>25h" 25 "awd" "8 Speed<br><em>Automatic/Manua… 108900 "Audi RS 7"
"Germany" "Audi" "S7" 2016 "Prestige quattro Hatchback" "hatchback" "450<br>@5800rpm" 5800 "406<br>@1400rpm" 1400 "17c<br>27h" 27 "awd" "7 Speed<br><em>Automatic</em>" 82900 "Audi S7"
"Germany" "Audi" "S6" 2016 "Premium Plus quattro Sedan" "sedan" "450<br>@5800rpm" 5800 "406<br>@1400rpm" 1400 "18c<br>27h" 27 "awd" "7 Speed<br><em>Automatic</em>" 70900 "Audi S6"
(
    GT(gtcars_final, groupname_col="ctry_origin", rowname_col="c_label")
    .cols_hide(
        ["mfr", "model", "drivetrain", "bdy_style", "mpg_h", "hp_rpm", "trq_rpm"]
    )
    .cols_move(["trsmn", "mpg_c"], after="trim")
    .tab_spanner(label="Performance", columns=["mpg_c", "hp", "trq"])
    .cols_label(**col_labels)
    .fmt_currency("msrp", decimals=0)
    .cols_align(align="center", columns=["mpg_c", "hp", "trq"])
    .tab_style(
        style=style.text(size=px("12")),
        locations=loc.body(columns=["trim", "trsmn", "mpg_c", "hp", "trq"]),
    )
    .tab_header(
        title=md("The Cars of **gtcars**"), subtitle="These are some fine automobiles"
    )
    .tab_source_note(md("Source: Various pages within the Edmonds website."))
)
The Cars of gtcars
These are some fine automobiles
Year Trim Transmission Performance MSRP
MPG HP Torque
Germany
Audi R8 2015 4.2 (Manual) Coupe 6 Speed
Manual
11c
20h
430
@7900rpm
317
@4500rpm
$115,900
Audi S8 2016 Base Sedan 8 Speed
Automatic/Manual
15c
25h
520
@5800rpm
481
@1700rpm
$114,900
Audi RS 7 2016 Quattro Hatchback 8 Speed
Automatic/Manual
15c
25h
560
@5700rpm
516
@1750rpm
$108,900
Audi S7 2016 Prestige quattro Hatchback 7 Speed
Automatic
17c
27h
450
@5800rpm
406
@1400rpm
$82,900
Audi S6 2016 Premium Plus quattro Sedan 7 Speed
Automatic
18c
27h
450
@5800rpm
406
@1400rpm
$70,900
BMW i8 2016 Mega World Coupe 6 Speed
Automatic/Manual
28c
29h
357
@5800rpm
420
@3700rpm
$140,700
BMW M6 2016 Base Coupe 7 Speed
Automatic
15c
22h
560
@6000rpm
500
@1500rpm
$113,400
BMW M5 2016 Base Sedan 7 Speed
Automatic/Manual
15c
22h
560
@6000rpm
500
@1500rpm
$94,100
BMW 6-Series 2016 640 I Coupe 8 Speed
Automatic/Manual
20c
30h
315
@5800rpm
330
@1400rpm
$77,300
BMW M4 2016 Base Coupe 6 Speed
Manual
17c
24h
425
@5500rpm
406
@1850rpm
$65,700
Mercedes-Benz AMG GT 2016 S Coupe 7 Speed
Automatic
16c
22h
503
@6250rpm
479
@1750rpm
$129,900
Mercedes-Benz SL-Class 2016 SL400 Convertible 7 Speed
Automatic/Manual
20c
27h
329
@5250rpm
354
@1600rpm
$85,050
Porsche 911 2016 Carrera Coupe 7 Speed
Manual
20c
28h
350
@7400rpm
287
@5600rpm
$84,300
Porsche Panamera 2016 Base Sedan 7 Speed
Automatic
18c
28h
310
@6200rpm
295
@3750rpm
$78,100
Porsche 718 Boxster 2017 Base Convertible 6 Speed
Manual
21c
28h
300
@6500rpm
280
@1950rpm
$56,000
Porsche 718 Cayman 2017 Base Coupe 6 Speed
Manual
20c
29h
300
@6500rpm
280
@1950rpm
$53,900
Italy
Ferrari LaFerrari 2015 Base Coupe 7 Speed
Automatic
12c
16h
949
@9000rpm
664
@6750rpm
$1,416,362
Ferrari F12Berlinetta 2015 Base Coupe 7 Speed
Automatic
11c
16h
731
@8250rpm
509
@6000rpm
$319,995
Ferrari GTC4Lusso 2017 Base Coupe 7 Speed
Automatic
12c
17h
680
@8250rpm
514
@5750rpm
$298,000
Ferrari FF 2015 Base Coupe 7 Speed
Automatic
11c
16h
652
@8000rpm
504
@6000rpm
$295,000
Ferrari 458 Speciale 2015 Base Coupe 7 Speed
Automatic
13c
17h
597
@9000rpm
398
@6000rpm
$291,744
Ferrari 458 Spider 2015 Base 7 Speed
Automatic
13c
17h
562
@9000rpm
398
@6000rpm
$263,553
Ferrari 488 GTB 2016 Base Coupe 7 Speed
Automatic
15c
22h
661
@8000rpm
561
@3000rpm
$245,400
Ferrari 458 Italia 2014 Base Coupe 7 Speed
Automatic
13c
17h
562
@9000rpm
398
@6000rpm
$233,509
Ferrari California 2015 Base Convertible 7 Speed
Automatic
16c
23h
553
@7500rpm
557
@4750rpm
$198,973
Lamborghini Aventador 2015 LP 700-4 Coupe 7 Speed
Automatic
11c
18h
700
@8250rpm
507
@5500rpm
$397,500
Lamborghini Huracan 2015 LP 610-4 Coupe 7 Speed
Automatic
16c
20h
610
@8250rpm
413
@6500rpm
$237,250
Lamborghini Gallardo 2014 LP 550-2 Coupe 6 Speed
Automatic
12c
20h
550
@8000rpm
398
@6500rpm
$191,900
Maserati Granturismo 2016 Sport Coupe 6 Speed
Automatic/Manual
13c
21h
454
@7600rpm
384
@4750rpm
$132,825
Maserati Quattroporte 2016 S Sedan 8 Speed
Automatic/Manual
16c
23h
404
@5500rpm
406
@1500rpm
$99,900
Maserati Ghibli 2016 Base Sedan 8 Speed
Automatic/Manual
17c
24h
345
@5250rpm
369
@1750rpm
$70,600
United States
Chevrolet Corvette 2016 Z06 Coupe 7 Speed
Manual
15c
22h
650
@6400rpm
650
@3600rpm
$88,345
Dodge Viper 2017 GT Coupe 6 Speed
Manual
12c
19h
645
@5000rpm
600
@5000rpm
$95,895
Ford GT 2017 Base Coupe 7 Speed
Automatic
11c
18h
647
@6250rpm
550
@5900rpm
$447,000
Tesla Model S 2017 75D 1 Speed
Direct Drive
None 259
@6100rpm
None $74,500
Japan
Acura NSX 2017 Base Coupe 9 Speed
Automatic
21c
22h
573
@6500rpm
476
@2000rpm
$156,000
Nissan GT-R 2016 Premium Coupe 6 Speed
Automatic
16c
22h
545
@6400rpm
436
@3200rpm
$101,770
United Kingdom
Aston Martin Vanquish 2016 Base Coupe 8 Speed
Automatic/Manual
13c
21h
568
@6650rpm
465
@5500rpm
$287,250
Aston Martin DB11 2017 Base Coupe 8 Speed
Automatic/Manual
15c
21h
608
@6500rpm
516
@1500rpm
$211,195
Aston Martin Rapide S 2016 Base Sedan 8 Speed
Automatic/Manual
14c
21h
552
@6650rpm
465
@5500rpm
$205,300
Aston Martin Vantage 2016 V8 GT (Manual) Coupe 6 Speed
Manual
13c
19h
430
@7300rpm
361
@5000rpm
$103,300
Bentley Continental GT 2016 V8 Coupe 8 Speed
Automatic/Manual
15c
25h
500
@6000rpm
487
@1700rpm
$198,500
Jaguar F-Type 2016 Base (Manual) Coupe 6 Speed
Manual
16c
24h
340
@6500rpm
332
@3500rpm
$65,000
Lotus Evora 2017 2+2 Coupe 6 Speed
Manual
16c
24h
400
@7000rpm
302
@3500rpm
$91,900
McLaren 570 2016 Base Coupe 7 Speed
Automatic
16c
23h
570
@7500rpm
443
@5000rpm
$184,900
Rolls-Royce Dawn 2016 Base Convertible 8 Speed
Automatic
12c
19h
563
@5250rpm
575
@1500rpm
$335,000
Rolls-Royce Wraith 2016 Base Coupe 8 Speed
Automatic
13c
21h
624
@5600rpm
590
@1500rpm
$304,350
Source: Various pages within the Edmonds website.