Introduction to the great_tables library in Python

logo of a chart:TableBig

This post delves into the fundamental aspects of the great_tables library in Python. It provides a detailed explanation on creating tables using the great_tables library.

Additionally, it demonstrates how to add a title, a subtitle, and source notes to your table. It also guides you on how to format the text using markdown.

For a more advanced use case, check how to customize tables using great_tables.

About great_tables

The great_tables package in Python is designed to help users create visually appealing and highly customizable tables with ease. By leveraging cohesive table components like headers, footers, and formatted cells, it transforms data from Pandas or Polars DataFrames into elegant tables.

The package emphasizes simplicity for everyday needs while offering advanced customization options when required. Ideal for displaying data in various formats, great_tables is perfect for both console outputs and notebook environments, making it a versatile tool for data presentation.

It's quite similar, and its syntax resembles that of the gt package in R.

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

# load dataset about pizza
from great_tables.data import pizzaplace as df
df = df[['name', 'size', 'type', 'price']].tail()
df.index = range(5)
df
name size type price
0 four_cheese L veggie 17.95
1 napolitana S classic 12.00
2 ckn_alfredo M chicken 16.75
3 mexicana L veggie 20.25
4 bbq_ckn S chicken 12.75

Most basic table with great_tables

The great_tables package offers a GT() function to create a specialized object, which enhances the functionality of our table.

To utilize it with our dataframe, follow these steps:

my_table = GT(df)
my_table
name size type price
four_cheese L veggie 17.95
napolitana S classic 12.0
ckn_alfredo M chicken 16.75
mexicana L veggie 20.25
bbq_ckn S chicken 12.75

Title and suptitle

The simplest way to add a title and subtitle is by using the tab_header() method from the GT object.

This function accepts title and subtitle arguments, producing an output that appears like this:

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 the tab_header()

my_table = (
   GT(df)
   .tab_header(
      title="Pizza 🍕",
      subtitle="A list of pizza available in the Pyzza Graph Gallery",
   )
)
my_table
Pizza 🍕
A list of pizza available in the Pyzza Graph Gallery
name size type price
four_cheese L veggie 17.95
napolitana S classic 12.0
ckn_alfredo M chicken 16.75
mexicana L veggie 20.25
bbq_ckn S chicken 12.75

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
name size type price
four_cheese L veggie 17.95
napolitana S classic 12.0
ckn_alfredo M chicken 16.75
mexicana L veggie 20.25
bbq_ckn S chicken 12.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:

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!