Creating Tables in Python with Pandas, Plottable, and Matplotlib

Introduction to Tables

Tables organize data in to rows and columns, facilitating sorting, filtering, and analysis. They provide a clear and structured way to present information, enabling quick comparisons and insights.

Generating Tables with Pandas

import pandas as pd
import numpy as np

# Generate sample data
num_samples = 6

city_a = np.random.uniform(15, 55, num_samples)
city_b = np.random.uniform(18, 42, num_samples)
city_c = np.random.uniform(8, 28, num_samples)

df_data = pd.DataFrame({
    'City_A': city_a,
    'City_B': city_b,
    'City_C': city_c
}, index=pd.date_range(start='2020-01-01', periods=num_samples).strftime('%d-%m-%Y'))

# Define styling function
def apply_styling(styler_obj):
    styler_obj.background_gradient(cmap='Greens', axis=None)
    styler_obj.format(precision=2)
    styler_obj.applymap(lambda val: 'text-align: center; font-size: 12px;')
    return styler_obj

summary_stats = df_data.agg(['mean', 'max'])
combined_df = pd.concat([df_data, summary_stats])
styled_table = combined_df.style.pipe(apply_styling)

Creating Tables with Plottable

import matplotlib.pyplot as plt
import pandas as pd
from plottable import Table

# Prepare dataset
dataset = {
    'Rating': [7, 5, 9, 4, 8],
    'Amount': [45.30, 22.15, 38.75, 12.40, 85.20],
    'Note': ['Good', 'Fine', 'Great', 'Poor', 'OK']
}
df_plot = pd.DataFrame(dataset)

fig, axis = plt.subplots(figsize=(6, 4))
table_obj = Table(df_plot)
plt.show()

Building Tables with Matplotlib

import matplotlib.pyplot as plt
import numpy as np

# Define data
data_values = {
    'Cold': [55000, 185000, 82000, 490000, 28000],
    'Gale': [62000, 395000, 71000, 105000, 145000],
    'Storm': [95000, 88000, 162000, 510000, 595000],
    'Shake': [81000, 92000, 142000, 204000, 75000],
    'Ice': [128000, 345000, 332000, 765000, 48000]
}
row_labels = [f'{yr} years' for yr in range(1, 6)]

fig, ax = plt.subplots(figsize=(8, 5))
ax.axis('tight')
ax.axis('off')
table = ax.table(cellText=[list(data_values[key]) for key in data_values],
                 rowLabels=row_labels,
                 colLabels=list(data_values.keys()),
                 loc='center')
table.auto_set_font_size(False)
table.set_fontsize(10)
table.scale(1.2, 1.5)
plt.show()

Tags: python Data Visualization Pandas matplotlib tables

Posted on Tue, 26 May 2026 01:01:00 +0000 by alexville