Level Up Your Python Tables: Structure, Style, and Clarity with Great Tables
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:
- A structure that makes them easy to read
- Well-formatted values to make data understandable
- Styling that draws the viewer’s attention to key takeaways
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% | ||
| Moka pot | $2,045,250 | 7% | $181,080 | 1% | ||
| Cold brew | $288,750 | 1% | $241,770 | 2% | ||
| Filter | $404,250 | 1% | $70,010 | 0% | ||
| Drip machine | $2,632,000 | 9% | $1,374,450 | 9% | ||
| AeroPress | $2,601,500 | 9% | $1,293,780 | 9% | ||
| Pour over | $846,000 | 3% | $364,530 | 2% | ||
| French press | $1,113,250 | 4% | $748,120 | 5% | ||
| Cezve | $2,512,500 | 9% | $1,969,520 | 13% | ||
| Chemex | $3,137,250 | 11% | $817,680 | 6% | ||
| Scale | $3,801,000 | 13% | $2,910,290 | 20% | ||
| Kettle | $756,250 | 3% | $617,520 | 4% | ||
| Espresso Machine | $8,406,000 | 29% | $3,636,440 | 25% | ||
| 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
This video includes:
- Examples of nicely formatted tables
- An introduction to the display table workflow:
- Structure for adding pieces like adding a title, column spanners, cleaning up column labels, creating row labels (stub), and grouping rows
- Format for cleaning up the values within the data cells
- Style for customizing the visual appearance of the table
- Other exciting features that enhance table display, such as nanoplots and images
Great Tables 2: Introducing Units Notation
This video includes:
- An introduction to unit notation and syntax
- Formatting methods
- Information on how to handle missing numbers
- Instructions on hiding columns
Great Tables 3: Data Color and Polishing
This video includes:
- Methods to transform a “wall of numbers” into a visually insightful heat map
- How to use Markdown for formatting
- Polars DataFrames and convenient column selectors
- Resources for further learning
Continue your Great Tables journey
We hope that you enjoy Michael and Rich’s video series! The fun doesn’t have to stop here.
- Explore the Great Tables package documentation.
- If you have questions, thoughts, or just want to chat, join the Great Tables Discord!