Level Up Your Python Tables: Structure, Style, and Clarity with Great Tables

2023-01-13

Get our email updates

Interested in learning more about Posit + Python tools? Join our email list.

Great Tables is a fantastic Python library that lets you create beautiful display tables. This means transforming a raw data structure, like a Polars DataFrame, into a formatted, styled, beautified table suitable for reports and publishing insights.

You can get started with a single line of code:

import polars as pl
import polars.selectors as cs
from great_tables import GT, loc, style

coffee_sales = pl.read_ndjson("https://raw.githubusercontent.com/posit-dev/great-tables/4a11db5692a6e223b108dfcb73b39885652ccefb/docs/examples/_data/coffee-sales.ndjson")

GT(coffee_sales)
icon product revenue_dollars revenue_pct profit_dollars profit_pct monthly_sales
grinder.png Grinder 904500.0 0.03 567960.0 0.04 [521, 494, 596, 613, 667, 748, 765, 686, 607, 594, 568, 751]
moka-pot.png Moka pot 2045250.0 0.07 181080.0 0.01 [4726, 4741, 4791, 5506, 6156, 6619, 6868, 6026, 5304, 4884, 4648, 6283]
cold-brew.png Cold brew 288750.0 0.01 241770.0 0.02 [244, 249, 438, 981, 1774, 2699, 2606, 2348, 1741, 896, 499, 244]
filter.png Filter 404250.0 0.01 70010.0 0.0 [2067, 1809, 1836, 2123, 2252, 2631, 2562, 2367, 2164, 2195, 2070, 2744]
drip-machine.png Drip machine 2632000.0 0.09 1374450.0 0.09 [2137, 1623, 1971, 2097, 2580, 2456, 2336, 2316, 2052, 1967, 1837, 2328]
aeropress.png AeroPress 2601500.0 0.09 1293780.0 0.09 [6332, 5199, 6367, 7024, 7906, 8704, 8693, 7797, 6828, 6963, 6877, 9270]
pour-over.png Pour over 846000.0 0.03 364530.0 0.02 [1562, 1291, 1511, 1687, 1940, 2177, 2141, 1856, 1715, 1806, 1601, 2165]
french-press.png French press 1113250.0 0.04 748120.0 0.05 [3507, 2880, 3346, 3792, 3905, 4095, 4184, 4428, 3279, 3420, 3297, 4819]
cezve.png Cezve 2512500.0 0.09 1969520.0 0.13 [12171, 11469, 11788, 13630, 15391, 16532, 17090, 14433, 12985, 12935, 11598, 15895]
chemex.png Chemex 3137250.0 0.11 817680.0 0.06 [4938, 4167, 5235, 6000, 6358, 6768, 7112, 6249, 5605, 6076, 4980, 7220]
scale.png Scale 3801000.0 0.13 2910290.0 0.2 [1542, 1566, 1681, 2028, 2425, 2549, 2569, 2232, 2036, 2089, 1693, 3180]
kettle.png Kettle 756250.0 0.03 617520.0 0.04 [1139, 1023, 1087, 1131, 1414, 1478, 1456, 1304, 1140, 1233, 1193, 1529]
espresso-machine.png Espresso Machine 8406000.0 0.29 3636440.0 0.25 [686, 840, 618, 598, 2148, 533, 797, 996, 1002, 668, 858, 2577]
None Total 29448500.0 1.0 14793150.0 1.0 None

However, we believe that effective display tables go beyond the default with:

As “total fanatics about table display,” Michael Chow and Richard Iannone, the minds behind Great Tables, understand this deeply. In their latest video series, they walk through more advanced techniques to elevate your Great Tables with structure, formatting, and styling.

For instance, see how Great Tables code can transform our basic table:

Code
gt_leveled_up = (
    GT(coffee_sales)
    .tab_header(title="Coffee Equipment Sales for 2023")
    .tab_spanner(label="Revenue", columns=cs.starts_with("revenue"))
    .tab_spanner(label="Profit", columns=cs.starts_with("profit"))
    .cols_label(
        revenue_dollars="Amount",
        revenue_pct="Percent",
        profit_dollars="Amount",
        profit_pct="Percent",
        monthly_sales="Monthly Sales",
    )
    .fmt_currency(columns=cs.ends_with("dollars"), use_subunits=False)
    .fmt_percent(columns=cs.ends_with("pct"), decimals=0)
    .tab_style(
        style=style.fill(color="aliceblue"),
        locations=loc.body(columns=cs.starts_with("revenue")),
    )
    .tab_style(
        style=style.fill(color="papayawhip"),
        locations=loc.body(columns=cs.starts_with("profit")),
    )
    .tab_style(
        style=style.text(weight="bold"),
        locations=loc.body(rows=pl.col("product") == "Total"),
    )
    .fmt_nanoplot(columns="monthly_sales", plot_type="bar")
    .fmt_image(
        columns="icon",
        path="~/rrr/posit-web/wp-content/themes/Posit/markdown-blogs/level-up-great-tables/img/",
    )
)

gt_leveled_up
Coffee Equipment Sales for 2023
icon product Revenue Profit Monthly Sales
Amount Percent Amount Percent
Grinder $904,500 3% $567,960 4%
7650521494596613667748765686607594568751
Moka pot $2,045,250 7% $181,080 1%
6.87K04.73K4.74K4.79K5.51K6.16K6.62K6.87K6.03K5.30K4.88K4.65K6.28K
Cold brew $288,750 1% $241,770 2%
2.70K02442494389811.77K2.70K2.61K2.35K1.74K896499244
Filter $404,250 1% $70,010 0%
2.74K02.07K1.81K1.84K2.12K2.25K2.63K2.56K2.37K2.16K2.19K2.07K2.74K
Drip machine $2,632,000 9% $1,374,450 9%
2.58K02.14K1.62K1.97K2.10K2.58K2.46K2.34K2.32K2.05K1.97K1.84K2.33K
AeroPress $2,601,500 9% $1,293,780 9%
9.27K06.33K5.20K6.37K7.02K7.91K8.70K8.69K7.80K6.83K6.96K6.88K9.27K
Pour over $846,000 3% $364,530 2%
2.18K01.56K1.29K1.51K1.69K1.94K2.18K2.14K1.86K1.72K1.81K1.60K2.16K
French press $1,113,250 4% $748,120 5%
4.82K03.51K2.88K3.35K3.79K3.90K4.10K4.18K4.43K3.28K3.42K3.30K4.82K
Cezve $2,512,500 9% $1,969,520 13%
17.1K012.2K11.5K11.8K13.6K15.4K16.5K17.1K14.4K13.0K12.9K11.6K15.9K
Chemex $3,137,250 11% $817,680 6%
7.22K04.94K4.17K5.24K6.00K6.36K6.77K7.11K6.25K5.60K6.08K4.98K7.22K
Scale $3,801,000 13% $2,910,290 20%
3.18K01.54K1.57K1.68K2.03K2.42K2.55K2.57K2.23K2.04K2.09K1.69K3.18K
Kettle $756,250 3% $617,520 4%
1.53K01.14K1.02K1.09K1.13K1.41K1.48K1.46K1.30K1.14K1.23K1.19K1.53K
Espresso Machine $8,406,000 29% $3,636,440 25%
2.58K06868406185982.15K5337979961.00K6688582.58K
None Total $29,448,500 100% $14,793,150 100% None

Ready to level up your tables? Explore the series details below, or start watching it here!

Great Tables 1: Structure, Format, and Style

Video Link | Workshop Repo

This video includes:

Great Tables 2: Introducing Units Notation

Video Link | Workshop Repo

This video includes:

Great Tables 3: Data Color and Polishing

Video Link | Workshop Repo

This video includes:

Continue your Great Tables journey

We hope that you enjoy Michael and Rich’s video series! The fun doesn’t have to stop here.