Creating elegant tables with the Pandas library in Python is a useful way to organize and display structured data. Pandas tables allow you to present information in a neat and organized format, making it easy to analyze and understand various datasets.
You can use Pandas to create tables that display data such as numerical values, text, and categorical information. These tables can be customized and styled to enhance their visual appeal, making them a versatile tool for data presentation and analysis.

Libraries

First, you need to install the following librairies:

  • matplotlib is used for plot creating the charts
  • pandas is used to put the data into a dataframe and custom the table
  • numpy is used to generate some data

For some reasons, we need to use an older pandas version (1.5.3) than the lastest available. It means we have to run pip install pandas==1.5.3 in the terminal before.

import pandas as pd
import matplotlib as mpl

# data generation
import numpy as np

Dataset

When creating nice output tables, we first need to have the dataframe with the values we want.

In this post, we'll use fake weather data from different cities. We'll take a look at different simple features of pandas to make this table more aesthetically appealing.

sample_size = 6

new_york = np.random.uniform(20,60,sample_size)
paris = np.random.uniform(20,40,sample_size)
london = np.random.uniform(5,30,sample_size)

df = pd.DataFrame({'new_york': new_york,
                   'paris': paris,
                   'london': london},
                 
                 # generate date values in the index of the dataframe
                 index=pd.date_range(start="2020-01-01", periods=sample_size).strftime("%d-%m-%Y"))

Default output

The default result isn't very pretty, but it's from this base that we'll build something more pleasing to the eye.

df
new_york paris london
01-01-2020 23.620452 24.961700 15.059958
02-01-2020 53.725231 30.954143 21.996464
03-01-2020 52.312884 28.618204 23.748652
04-01-2020 29.841392 21.862396 18.777793
05-01-2020 52.857303 24.430124 10.540992
06-01-2020 52.577723 31.604297 10.159980

Change colors

A clean way to apply modifications to Pandas tables is to create a function that performs the modifications, then apply this function to our data frame.

  • We use a colormap named "Reds", that will put the background color of each cell depending on the value in the cell.

Once the function is defined, we use the style() and pipe() from Pandas to apply this function to the dataframe.

def custom_table(styler):
    styler.background_gradient(cmap="Reds", axis=None)
    
    # center text and increase font size
    styler.applymap(lambda x: 'text-align: center; font-size: 14px;') 
    return styler

df.style.pipe(custom_table)
  new_york paris london
01-01-2020 23.620452 24.961700 15.059958
02-01-2020 53.725231 30.954143 21.996464
03-01-2020 52.312884 28.618204 23.748652
04-01-2020 29.841392 21.862396 18.777793
05-01-2020 52.857303 24.430124 10.540992
06-01-2020 52.577723 31.604297 10.159980

Here, for example, it's much easier to see that temperatures in New York are higher compared to London.

Add aggregate metrics

To make our table more meaningful, we can then add aggregation measures. To do this, we use pandas' agg() and concat() functions.

We also add a line to our function to round off the values in our table, using the format() function.

def custom_table(styler):
    styler.background_gradient(cmap="Blues", axis=None)
    styler.format(precision=2)

    # center text and increase font size
    styler.applymap(lambda x: 'text-align: center; font-size: 14px;') 
    return styler

agg_metrics = df.agg(["mean", "max"])
pd.concat([df, agg_metrics]).style.pipe(custom_table)
  new_york paris london
01-01-2020 23.62 24.96 15.06
02-01-2020 53.73 30.95 22.00
03-01-2020 52.31 28.62 23.75
04-01-2020 29.84 21.86 18.78
05-01-2020 52.86 24.43 10.54
06-01-2020 52.58 31.60 10.16
mean 44.16 27.07 16.71
max 53.73 31.60 23.75

Going further

This post explains how to create a simple custom table with pandas.

For more examples of how to create or customize your tables, see the table section. You may also be interested in how to add HTML and CCS to your table.

Animation with python

Animation

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 🙏!