Libraries

First, you need to install the following librairies:

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_yorkparislondon
01-01-202023.62045224.96170015.059958
02-01-202053.72523130.95414321.996464
03-01-202052.31288428.61820423.748652
04-01-202029.84139221.86239618.777793
05-01-202052.85730324.43012410.540992
06-01-202052.57772331.60429710.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.

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_yorkparislondon
01-01-202023.62045224.96170015.059958
02-01-202053.72523130.95414321.996464
03-01-202052.31288428.61820423.748652
04-01-202029.84139221.86239618.777793
05-01-202052.85730324.43012410.540992
06-01-202052.57772331.60429710.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_yorkparislondon
01-01-202023.6224.9615.06
02-01-202053.7330.9522.00
03-01-202052.3128.6223.75
04-01-202029.8421.8618.78
05-01-202052.8624.4310.54
06-01-202052.5831.6010.16
mean44.1627.0716.71
max53.7331.6023.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 build beautiful tables with great_tables.