Libraries & Dataset

For creating this chart, we will only need to load great_tables!

You can install it via the pip install great_tables command.

Also, since great_tables provides a few datasets, we're going to use them directly.

from great_tables import GT, md, style, loc

# load dataset about pizza
from great_tables.data import pizzaplace as df
df = df[['name', 'size', 'type', 'price']].tail()
df.index = range(5)
df
namesizetypeprice
0four_cheeseLveggie17.95
1napolitanaSclassic12.00
2ckn_alfredoMchicken16.75
3mexicanaLveggie20.25
4bbq_cknSchicken12.75

Simple table with great_tables

Let's start with a simple table with a header and a footer:

my_table = (
   GT(df)
   .tab_header(
      title="🍕 Pizza 🍕",
      subtitle="A list of pizza available in the Pyzza Graph Gallery",
   )
   .tab_source_note(md("**Data source**: the `great_tables` python library"))
   .tab_source_note(md("**Tutorial**: the *[Python Graph Gallery](https://python-graph-gallery.com)*"))
)
my_table
🍕 Pizza 🍕
A list of pizza available in the Pyzza Graph Gallery
namesizetypeprice
four_cheeseLveggie17.95
napolitanaSclassic12.0
ckn_alfredoMchicken16.75
mexicanaLveggie20.25
bbq_cknSchicken12.75
Data source: the great_tables python library
Tutorial: the Python Graph Gallery

Color some row/column

We can color a specific row/col using the tab_style() function. This functions requires 2 arguments:

_Note that the parenthesis around the code are only here to make the code easier to read. Without it, we would not be able to jump row when calling a tab\__() function*

my_table = (
   GT(df, rowname_col="name")
   .tab_header(
      title="Pizza 🍕",
      subtitle="A list of pizza available in the Pyzza Graph Gallery",
   )
   .tab_source_note(md("**Data source**: the `great_tables` python library"))
   .tab_source_note(md("**Tutorial**: the *Python Graph Gallery*"))
   .tab_style(
      style=style.fill(color="#f0c580"),
      locations=loc.body(columns="price", rows=["mexicana"]),
   )
   .tab_style(
      style=style.fill(color="#c5eceb"),
      locations=loc.body(columns="size", rows=["napolitana"]),
   )
   .tab_style(
      style=style.text(weight='bold', size='20px'),
      locations=loc.body(columns="price", rows=["ckn_alfredo"]),
   )
   .tab_style(
      style=style.borders(sides=["top", "left"], weight='3px', color="red"),
      locations=loc.body(columns="type", rows=[3]),
   )
)
my_table
Pizza 🍕
A list of pizza available in the Pyzza Graph Gallery
sizetypeprice
four_cheeseLveggie17.95
napolitanaSclassic12.0
ckn_alfredoMchicken16.75
mexicanaLveggie20.25
bbq_cknSchicken12.75
Data source: the great_tables python library
Tutorial: the Python Graph Gallery

Footer

To add a footer, we need to use the tab_source_note() function.

We can wrap our text with the md() function to include markdown formatting such as bold, code-like, and italic styles.

my_table = (
   GT(df)
   .tab_header(
      title="🍕 Pizza 🍕",
      subtitle="A list of pizza available in the Pyzza Graph Gallery",
   )
   .tab_source_note(md("**Data source**: the `great_tables` python library"))
   .tab_source_note(md("**Tutorial**: the *Python Graph Gallery*"))
)
my_table
🍕 Pizza 🍕
A list of pizza available in the Pyzza Graph Gallery
namesizetypeprice
four_cheeseLveggie17.95
napolitanaSclassic12.0
ckn_alfredoMchicken16.75
mexicanaLveggie20.25
bbq_cknSchicken12.75
Data source: the great_tables python library
Tutorial: the Python Graph Gallery

Going further

This article explains how to use the great_tables library in Python.

You might be interested in the table section of the gallery to learn everything about tables in python