Lesson 58 · Mastering Pandas
How to Create an Interactive KPI Dashboard Using Python and Pandas
Welcome to this intermediate-beginner lesson on building KPI dashboards in Python with Pandas. Key Performance Indicators (KPIs) are vital numbers that help…
- CourseMastering Pandas
- Lesson58 of 44
- Video21 min
- FormatJupyter notebook · 15 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbBuilding a KPI Dashboard with Pandas#
Welcome to this intermediate-beginner lesson on building KPI dashboards in Python with Pandas. Key Performance Indicators (KPIs) are vital numbers that help organizations measure progress. In this lesson, we will build simple but powerful dashboard metrics using Pandas.
We will use the famous Titanic dataset to explore, clean, analyze, and visualize data. Lets get started!
import warnings
import numpy as np
np.random.seed(42)
warnings.filterwarnings("ignore") # Suppress warnings for a cleaner lesson
# Data setup (Titanic Dataset)
import pandas as pd
url = 'https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv'
df = pd.read_csv(url)
print(df.shape)
print(df.head(3))
What is a DataFrame?#
Pandas DataFrame is like a table of data with rows and columns. It holds our dataset, so we can select, filter, summarize, and plot with ease.
Lets check out some basic info about our Titanic data.
# Preview key info and columns
print(df.info())
print(df.columns)
print(df.describe())
Data Cleaning Basics#
Real-world data often has missing values or inconsistent formats. We want our KPIs to be based on clean data. First, lets find and handle missing values.
# Check for missing values
print(df.isnull().sum())
# Fill missing Age values with the median age
df['Age'] = df['Age'].fillna(df['Age'].median())
# Remove rows with missing Embarked values
df = df.dropna(subset=['Embarked'])
print('After cleaning:')
print(df.isnull().sum())
# Convert data types for categories
df['Pclass'] = df['Pclass'].astype('category')
df['Sex'] = df['Sex'].astype('category')
df['Embarked'] = df['Embarked'].astype('category')
Creating KPI Columns#
KPIs should be clear and actionable. Lets create a few columns that we will use for our dashboard metrics.
# Create 'IsChild' KPI: Age under 16
df['IsChild'] = df['Age'] < 16
# Create 'FamilySize' KPI: count siblings, spouses, parents, children
df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
# Filtering examples: Women, Children, Third class
women = df[df['Sex'] == 'female']
children = df[df['IsChild']]
third_class = df[df['Pclass'] == 3]
print(f"Number of women: {women.shape[0]}")
print(f"Number of children: {children.shape[0]}")
print(f"Number in third class: {third_class.shape[0]}")
Aggregating KPIs: Survival Rates#
KPIs often summarize data, like calculating rates per group. Lets check the overall survival rate and break it down by class and sex.
# Overall survival rate
survival_rate = df['Survived'].mean()
print(f"Overall survival rate: {survival_rate:.2%}")
# Survival by class
class_rates = df.groupby('Pclass')['Survived'].mean()
print("Survival rate by class:")
print(class_rates)
# Survival by sex
sex_rates = df.groupby('Sex')['Survived'].mean()
print("Survival rate by sex:")
print(sex_rates)
Advanced Grouping: Multi-dimensional KPIs#
Pandas lets us group by several columns at once. This is helpful for 'KPI tables' with multiple categories. Lets see survival rates by both class and sex.
# Group by class AND sex for a KPI 'matrix'
pivot = df.pivot_table('Survived', index='Pclass', columns='Sex', aggfunc='mean')
print(pivot)
# KPI: Average Fare by Embarked point and Class
kpi_fare = df.groupby(['Embarked', 'Pclass'])['Fare'].mean().unstack()
print(kpi_fare)
Adding Time-Based KPIs#
Dashboards often track metrics over time. Although Titanic does not have a proper timestamp, lets use the Ticket column's first character as a 'proxy' for time to create a demonstration.
# Create a 'FakeTime' column from ticket prefix
df['FakeTime'] = df['Ticket'].astype(str).str[0]
# Time-based KPI: mean age by FakeTime
age_time = df.groupby('FakeTime')['Age'].mean()
print(age_time)
# Visualize key KPIs with plots
import matplotlib.pyplot as plt
plt.figure(figsize=(7,5))
df.groupby('Pclass')['Survived'].mean().plot(kind='bar', color=['#1976D2', '#388E3C', '#FBC02D'])
plt.title('Survival Rate by Class')
plt.ylabel('Survival Rate')
plt.ylim(0, 1)
plt.show()
# Mini-project: Build a KPI summary dashboard
kpi_dashboard = pd.DataFrame({
'KPI': ['Total Passengers', 'Children', 'Women', 'Mean Fare', 'Overall Survival Rate'],
'Value': [
df.shape[0],
df['IsChild'].sum(),
(df['Sex'] == 'female').sum(),
df['Fare'].mean(),
df['Survived'].mean(),
]
})
print(kpi_dashboard)
# Challenge: Add an interactive filter
embarked_choice = input("Type a port of embarkation (C, Q, S): ")
filtered = df[df['Embarked'] == embarked_choice]
print(f"Number of passengers who boarded at {embarked_choice}: {filtered.shape[0]}")
print(f"Survival rate at {embarked_choice}: {filtered['Survived'].mean():.2%}")
# Dashboard best practices: handle missing data gracefully
def safe_survival_rate(df):
if df.empty:
return float('nan')
return df['Survived'].mean()
silly_port = input("Try a non-existent port code: ")
filtered2 = df[df['Embarked'] == silly_port]
print(f"Rows: {filtered2.shape[0]}, Survival: {safe_survival_rate(filtered2)}")
Recap: What We Learned#
- Loaded and explored Titanic data
- Cleaned and converted columns for analysis
- Created custom KPIs
- Grouped and summarized key business metrics
- Built and visualized a KPI dashboard
- Added interactivity and handled missing data safely
Try building a similar dashboard with your own data or datasets from public sources. Practicing with new data is how you will master these techniques!
Enjoyed this Lesson?#
If you found this useful, please like the video and subscribe to our channel. Comment with your KPI dashboard ideas or questionswe love hearing from you! Happy coding and see you soon!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



