How to Use Great Tables for Beautiful Data Presentation in Python

great tables beautiful data presentation python
Image by Author
 

Great Tables is a Python package that transforms ordinary data into publication-ready tables with polished formatting and styling. Whether you need to create summary statistics, format financial reports, or present research findings, Great Tables provides an intuitive interface for building professional tables that clearly communicate your data insights.

This tutorial walks through the essential features of Great Tables using built-in datasets. You’ll learn how to create basic tables, apply formatting, add summary rows, and customize styling to match your presentation needs.

Prerequisites and Setup

Great Tables works with Python 3.8 or higher and integrates seamlessly with pandas DataFrames. The package follows a method-chaining approach similar to popular Python libraries, making it intuitive if you’re already familiar with pandas or Plotly.

Install the package using pip:

 
pip install great-tables

For this tutorial, we’ll use the built-in gtcars dataset, which contains specifications for various car models including manufacturer, model name, year, horsepower, fuel efficiency, and pricing. The package includes 16 datasets designed for learning and experimentation.

Import the necessary components:

 
from great_tables import GT
from great_tables.data import gtcars
import pandas as pd

The GT class serves as the main constructor for creating tables, while the data module provides access to built-in datasets.

Creating Your First Table

The simplest way to start with Great Tables is to pass a pandas DataFrame to the GT() constructor. Let’s create a basic table showing a subset of car data:

 
cars_subset = gtcars[['mfr', 'model', 'year', 'hp', 'mpg_c']].head(8)
table = GT(cars_subset)
table

great tables beautiful data presentation python 
This displays an unformatted table with your data. You’ll see manufacturer, model, year, horsepower, and city MPG for the first eight vehicles in the dataset. The GT object serves as the foundation for all further customization.

Adding Table Components

Tables become more informative when you add descriptive elements. Great Tables lets you include a title, subtitle, and source note to provide context:

 
table = (
    GT(cars_subset)
    .tab_header(
        title="Performance Specifications",
        subtitle="Selected vehicles from 2014-2017"
    )
    .tab_source_note("Data source: gtcars dataset")
)
table

great tables beautiful data presentation python 
The header appears at the top of your table, centered and styled prominently, while the source note anchors at the bottom in smaller text. These elements help readers understand what data they’re viewing and where it comes from.

Formatting Numeric Columns

Raw numbers often need formatting for clarity and professional presentation. Great Tables provides dedicated methods for different data types, each designed to handle specific formatting needs.

Here we format horsepower as integers and miles per gallon with one decimal place:

 
table = (
    GT(cars_subset)
    .tab_header(title="Performance Specifications")
    .fmt_number(columns="hp", decimals=0)
    .fmt_number(columns="mpg_c", decimals=1)
    .cols_label(
        mfr="Manufacturer",
        hp="Horsepower",
        mpg_c="MPG (City)"
    )
)
table

great tables beautiful data presentation python 
The cols_label() method replaces default column names with reader-friendly labels, showing “Manufacturer” instead of “mfr” and “Horsepower” instead of “hp.” Numbers are now properly formatted with horsepower shown as whole numbers and MPG displayed with one decimal place. This makes your table immediately understandable without requiring knowledge of your dataset’s internal structure.

Beyond basic number formatting, Great Tables supports currency with fmt_currency(), percentages with fmt_percent(), and dates with fmt_date().

Creating Summary Statistics

Summary rows help readers quickly grasp aggregate patterns in your data without performing separate calculations. You can add statistics like means, totals, minimums, maximums, or counts to the bottom of your table.

Add calculations to your table:

 
summary_table = (
    GT(cars_subset)
    .tab_header(title="Vehicle Specifications with Averages")
    .fmt_number(columns=["hp", "mpg_c"], decimals=1)
    .cols_label(mfr="Manufacturer", hp="Horsepower", mpg_c="MPG")
    .grand_summary_rows(
        fns={"Average": lambda x: x[["hp", "mpg_c"]].mean()}
    )
)
summary_table

great tables beautiful data presentation python 
The summary row automatically calculates and displays the mean for horsepower and MPG columns, appearing at the bottom with a distinguishing border. The lambda function specifies which columns to summarize and what calculation to perform. This gives readers immediate context about typical values. You can specify multiple functions in the fns dictionary to show different statistical summaries at once.

Applying Custom Styling

Visual styling directs attention to important data and improves readability. Great Tables supports conditional formatting based on cell values, letting you create heat maps and highlight patterns automatically.

Apply color gradients to your data:

 
styled_table = (
    GT(cars_subset)
    .tab_header(title="Styled Performance Data")
    .fmt_number(columns=["hp", "mpg_c"], decimals=1)
    .data_color(
        columns="hp",
        palette=["lightblue", "darkblue"],
        domain=[100, 600]
    )
)
styled_table

great tables beautiful data presentation python 
The data_color() method applies a gradient based on horsepower values, creating a visual scale that makes high-performance vehicles immediately recognizable. Higher horsepower values appear in darker shades while lower values use lighter tones. The palette parameter accepts color names or hex codes, while domain sets the range for color mapping. This styling guides readers to meaningful patterns in the data without requiring them to compare individual numbers.

Complete Example with Multiple Features

Bringing these techniques together creates a comprehensive, publication-ready table. This workflow shows how to combine multiple formatting methods, summary statistics, and proper labeling into a single cohesive presentation.

Here’s the complete code:

 
final_table = (
    GT(gtcars[['mfr', 'model', 'hp', 'mpg_c', 'msrp']].head(10))
    .tab_header(
        title="Automotive Performance and Pricing",
        subtitle="Top 10 models by dataset order"
    )
    .cols_label(
        mfr="Manufacturer",
        hp="Horsepower", 
        mpg_c="City MPG",
        msrp="MSRP"
    )
    .fmt_number(columns=["hp", "mpg_c"], decimals=1)
    .fmt_currency(columns="msrp", currency="USD")
    .grand_summary_rows(
        fns={
            "Average": lambda x: x[["hp", "msrp"]].mean(),
            "Maximum": lambda x: x[["hp", "msrp"]].max()
        }
    )
    .tab_source_note("Source: gtcars dataset")
)
final_table

great tables beautiful data presentation python 
This table formats currency properly with dollar signs (showing values like “$447,000” for the Ford GT), displays horsepower and MPG with one decimal place, and includes two summary rows showing both average and maximum values for horsepower and MSRP. The title and subtitle provide context, while clear column labels and the source note make the table self-explanatory. The result is a professional table ready for reports, presentations, or publications.

Notice how method chaining keeps the code readable while building complexity step by step. Each method returns the modified table object, letting you continue adding features in a logical sequence.

Conclusion

Great Tables transforms data presentation by combining pandas DataFrames with flexible formatting options. The package’s method-chaining approach makes it easy to build complex tables incrementally, starting with basic structure and adding formatting and enhancements as you go.

Start with basic table creation using GT(), then add formatting with methods like fmt_number() and fmt_currency(). Include context through tab_header() and source notes to make your tables self-explanatory. Add readability with summary rows using grand_summary_rows() and apply visual styling with data_color() to highlight important patterns.

The package includes 15 other built-in datasets for experimentation, including financial data, geographic information, and scientific measurements. These datasets let you practice different table configurations and explore advanced features before applying them to your own data. Explore the official documentation for advanced features like row grouping, column spanners, and custom themes that provide even greater control over table appearance.

Leave a Reply

Your email address will not be published. Required fields are marked *