# Great Tables in Python: Make Publication-Quality Tables

> A Great Tables tutorial in Python: format a pandas DataFrame with GT, add headers, spanners and data_color, draw nanoplot sparklines and save the table as a PNG.

Source: https://www.pythoncharts.com/python/great-tables-python/ | Published: 2026-11-27 | Author: Alex Scott | Category: Python | Tags: great tables, table, pandas, formatting, sparklines

Great Tables is a Python library for making presentation-ready tables from a
pandas or Polars DataFrame. Wrap the data in `GT(df)`, chain methods such as
`tab_header()` and `fmt_currency()`, and save it with `gtsave()`. This post uses
Great Tables 1.2.0 (`pip install great_tables`) and pandas 3.0. Saving images
needs Chrome or another supported browser installed.

## Quick Example

```python
import pandas as pd
from great_tables import GT, loc, md, style

cafes = pd.DataFrame({
    "city": ["Porto", "Ghent", "Lund", "Graz", "Turku"],
    "cafes": [14, 9, 6, 8, 5],
    "revenue": [1_842_500, 1_203_900, 764_300, 918_750, 551_200],
    "growth": [0.124, 0.061, -0.038, 0.092, 0.017],
    "rating": [4.6, 4.4, 4.7, 4.2, 4.5],
})

table = (
    GT(cafes, rowname_col="city")
    .tab_header(title="Café performance, 2026",
                subtitle="Five cities, made-up data")
    .cols_label(cafes="Cafés", revenue="Revenue", growth="Growth",
                rating="Rating")
    .fmt_currency("revenue", currency="USD", decimals=0)
    .fmt_percent("growth", decimals=1, force_sign=True)
    .fmt_number("rating", decimals=1)
    .data_color(columns="growth", palette=["#f4a582", "#f7f7f7", "#92c5de"],
                domain=[-0.15, 0.15])
    .tab_source_note("Growth is year over year.")
)
table.gtsave("great-tables-quick-example.png")
table
```

![Formatted table of five cities with café counts, revenue in dollars, colored growth percentages and ratings](https://www.pythoncharts.com/python/great-tables-python/images/great-tables-quick-example.png)

In Jupyter, the last line displays the table. In a script, `gtsave()` writes the
file, and `table.show()` opens it in a browser.

## Group columns with spanners and rows with groups

`tab_spanner()` puts one heading over several columns. `groupname_col` groups
rows by a column.

```python
regional = cafes.assign(region=["South", "West", "North", "West", "North"])

grouped = (
    GT(regional, rowname_col="city", groupname_col="region")
    .tab_header(title="Café performance by region")
    .cols_label(cafes="Cafés", revenue="Revenue", growth="Growth",
                rating="Rating")
    .tab_spanner(label="Sales", columns=["revenue", "growth"])
    .tab_spanner(label="Operations", columns=["cafes", "rating"])
    .cols_move_to_end(["cafes", "rating"])
    .fmt_currency("revenue", compact=True, decimals=2)
    .fmt_percent("growth", decimals=1, force_sign=True)
)
grouped.gtsave("great-tables-spanners.png")
grouped
```

![Table with Sales and Operations headings over column pairs and rows grouped into South, West and North](https://www.pythoncharts.com/python/great-tables-python/images/great-tables-spanners.png)

`compact=True` shortens large values to forms like `$1.84M`.

## Highlight specific cells

`tab_style()` applies a style to a location. `loc.body()` takes columns and a row
condition.

```python
highlighted = (
    GT(cafes, rowname_col="city")
    .cols_label(cafes="Cafés", revenue="Revenue", growth="Growth",
                rating="Rating")
    .fmt_currency("revenue", decimals=0)
    .fmt_percent("growth", decimals=1)
    .tab_style(
        style=[style.fill("#fff3bf"), style.text(weight="bold")],
        locations=loc.body(
            columns="rating",
            rows=lambda df: df["rating"] == df["rating"].max()))
    .tab_style(
        style=style.text(color="#c92a2a"),
        locations=loc.body(columns="growth",
                           rows=lambda df: df["growth"] < 0))
    .tab_source_note(md("**Yellow:** best rating. **Red:** shrinking revenue."))
)
highlighted.gtsave("great-tables-highlight.png")
highlighted
```

![Table with the best rating highlighted in yellow and the negative growth value in red](https://www.pythoncharts.com/python/great-tables-python/images/great-tables-highlight.png)

## Add sparklines and bars inside cells

`fmt_nanoplot()` draws a mini chart from a column holding lists of numbers.

```python
trends = cafes[["city", "revenue"]].copy()
trends["monthly"] = [
    [128, 131, 140, 149, 155, 162, 171, 166, 158, 154, 160, 169],
    [96, 93, 99, 101, 104, 103, 108, 99, 101, 98, 100, 102],
    [70, 69, 68, 66, 67, 63, 62, 60, 61, 59, 60, 59],
    [68, 70, 74, 75, 79, 83, 82, 80, 77, 76, 78, 77],
    [44, 42, 45, 46, 47, 49, 50, 46, 45, 44, 46, 47],
]
trends["quarters"] = [[sum(m[i:i + 3]) for i in range(0, 12, 3)]
                      for m in trends.monthly]

mini = (
    GT(trends, rowname_col="city")
    .cols_label(revenue="Revenue", monthly="Monthly footfall",
                quarters="Quarterly footfall")
    .fmt_currency("revenue", decimals=0)
    .fmt_nanoplot("monthly")
    .fmt_nanoplot("quarters", plot_type="bar")
)
mini.gtsave("great-tables-nanoplots.png")
mini
```

![Table with a line sparkline of monthly values and a small bar chart of quarterly values in each row](https://www.pythoncharts.com/python/great-tables-python/images/great-tables-nanoplots.png)

Each row's mini chart scales to its own values by default. Pass
`autoscale=True` to give a column one shared y scale.

## Save as PNG, PDF or HTML

```python
table.gtsave("cafes@3x.png", zoom=3)      # sharper image
table.gtsave("cafes.pdf")
html = table.as_raw_html()                # for web pages and emails
```

## Great Tables, pandas Styler or Matplotlib?

| Need | Tool |
|---|---|
| Titles, spanners, footnotes and polished output | Great Tables |
| Quick conditional formatting in a notebook | [pandas Styler](https://www.pythoncharts.com/pandas/pandas-style-dataframe/) |
| A table inside a Matplotlib figure | `ax.table()` |
| Mini charts you design yourself | [Matplotlib sparklines](https://www.pythoncharts.com/matplotlib/sparklines-python/) |

## Practical tips

- Don't modify a DataFrame after passing it to `GT()`. In 1.2.0, adding a column to it afterward made the earlier table raise a `KeyError` when saved. Make a copy with `assign()` instead, as above.
- Format numbers in the table, not in the DataFrame, so values stay numeric.
- Show only the decimals readers need, and keep them consistent in a column.
- Color one or two columns. A table colored everywhere has no focus.
- Set `domain` in `data_color()` so a color means the same value in every report.

## Similar Topics

- [Style pandas DataFrames](https://www.pythoncharts.com/pandas/pandas-style-dataframe/)
- [Sparklines and Small Multiples in Python](https://www.pythoncharts.com/matplotlib/sparklines-python/)
- [Annotated Heatmaps in Matplotlib](https://www.pythoncharts.com/matplotlib/correlation-matrix-heatmap-annotations/)
