Mathew K Analytics

Lesson 68 · Data Science Projects

Building an Interactive Retail Sales Dashboard with Plotly and Streamlit in Python

Welcome! In this course, you will explore essential data mining concepts using Python. What is data mining? Data mining means discovering patterns within…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Data Mining with Python: Retail Sales Dashboard (Week 12)#

Welcome! In this course, you will explore essential data mining concepts using Python.

What is data mining? Data mining means discovering patterns within data. It is used in business, health, government, and more.

You will learn by working with real datasets, like Retail Supermarket Sales, Titanic survival, and Airbnb listings.

This week: Build foundations for your own interactive sales dashboard.

# Suppress warnings for clean outputs
import warnings; warnings.filterwarnings("ignore")
import numpy as np
np.random.seed(42)
# Data setup (Supermarket Sales Dataset)
import pandas as pd
url = 'https://raw.githubusercontent.com/juarezefren/datasets/main/supermarket_sales.csv'
df = pd.read_csv(url)
print(df.shape)
print(df.head(3))
(1000, 17)
    Invoice ID Branch       City Customer type  Gender  \
0  750-67-8428      A     Yangon        Member  Female   
1  226-31-3081      C  Naypyitaw        Normal  Female   
2  631-41-3108      A     Yangon        Normal    Male   

             Product line  Unit price  Quantity   Tax 5%     Total      Date  \
0       Health and beauty       74.69         7  26.1415  548.9715  1/5/2019   
1  Electronic accessories       15.28         5   3.8200   80.2200  3/8/2019   
2      Home and lifestyle       46.33         7  16.2155  340.5255  3/3/2019   

    Time      Payment    cogs  gross margin percentage  gross income  Rating  
0  13:08      Ewallet  522.83                 4.761905       26.1415     9.1  
1  10:29         Cash   76.40                 4.761905        3.8200     9.6  
2  13:23  Credit card  324.31                 4.761905       16.2155     7.4  

What is in the Supermarket Sales data?#

Each row is a single transaction at a supermarket.

Columns show information like:

  • Branch (store location)
  • Date and Time
  • Product Line
  • Customer Type
  • Gender
  • Total Sale

We will use this data to practice cleaning and basic analysis.

# See all column names
print(df.columns.tolist())
['Invoice ID', 'Branch', 'City', 'Customer type', 'Gender', 'Product line', 'Unit price', 'Quantity', 'Tax 5%', 'Total', 'Date', 'Time', 'Payment', 'cogs', 'gross margin percentage', 'gross income', 'Rating']
# Basic data info and types
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 17 columns):
 #   Column                   Non-Null Count  Dtype  
---  ------                   --------------  -----  
 0   Invoice ID               1000 non-null   object 
 1   Branch                   1000 non-null   object 
 2   City                     1000 non-null   object 
 3   Customer type            1000 non-null   object 
 4   Gender                   1000 non-null   object 
 5   Product line             1000 non-null   object 
 6   Unit price               1000 non-null   float64
 7   Quantity                 1000 non-null   int64  
 8   Tax 5%                   1000 non-null   float64
 9   Total                    1000 non-null   float64
 10  Date                     1000 non-null   object 
 11  Time                     1000 non-null   object 
 12  Payment                  1000 non-null   object 
 13  cogs                     1000 non-null   float64
 14  gross margin percentage  1000 non-null   float64
 15  gross income             1000 non-null   float64
 16  Rating                   1000 non-null   float64
dtypes: float64(7), int64(1), object(9)
memory usage: 132.9+ KB
# Look for missing values
print(df.isnull().sum())
Invoice ID                 0
Branch                     0
City                       0
Customer type              0
Gender                     0
Product line               0
Unit price                 0
Quantity                   0
Tax 5%                     0
Total                      0
Date                       0
Time                       0
Payment                    0
cogs                       0
gross margin percentage    0
gross income               0
Rating                     0
dtype: int64
# Parse 'Date' as datetime type
df['Date'] = pd.to_datetime(df['Date'])
print(df['Date'].dtype)
datetime64[ns]
# Clean whitespace in column names (if needed)
df.columns = df.columns.str.strip()
# Quick describe of number columns
print(df.describe())
        Unit price     Quantity       Tax 5%        Total  \
count  1000.000000  1000.000000  1000.000000  1000.000000   
mean     55.672130     5.510000    15.379369   322.966749   
min      10.080000     1.000000     0.508500    10.678500   
25%      32.875000     3.000000     5.924875   124.422375   
50%      55.230000     5.000000    12.088000   253.848000   
75%      77.935000     8.000000    22.445250   471.350250   
max      99.960000    10.000000    49.650000  1042.650000   
std      26.494628     2.923431    11.708825   245.885335   

                             Date        cogs  gross margin percentage  \
count                        1000  1000.00000              1000.000000   
mean   2019-02-14 00:05:45.600000   307.58738                 4.761905   
min           2019-01-01 00:00:00    10.17000                 4.761905   
25%           2019-01-24 00:00:00   118.49750                 4.761905   
50%           2019-02-13 00:00:00   241.76000                 4.761905   
75%           2019-03-08 00:00:00   448.90500                 4.761905   
max           2019-03-30 00:00:00   993.00000                 4.761905   
std                           NaN   234.17651                 0.000000   

       gross income      Rating  
count   1000.000000  1000.00000  
mean      15.379369     6.97270  
min        0.508500     4.00000  
25%        5.924875     5.50000  
50%       12.088000     7.00000  
75%       22.445250     8.50000  
max       49.650000    10.00000  
std       11.708825     1.71858  
# Find unique values for Branch and Product line
print(df['Branch'].unique())
print(df['Product line'].unique())
['A' 'C' 'B']
['Health and beauty' 'Electronic accessories' 'Home and lifestyle'
 'Sports and travel' 'Food and beverages' 'Fashion accessories']

Let us make our first sales plot!#

We can quickly see how sales change by day using a line chart.

Charts are great for finding trends you can show others.

import matplotlib.pyplot as plt
# Total sales per day
daily_sales = df.groupby('Date')['Total'].sum()
plt.figure(figsize=(10,4))
daily_sales.plot()
plt.xlabel('Date')
plt.ylabel('Total Sales')
plt.title('Total Sales Per Day')
plt.tight_layout()
plt.show()
No description has been provided for this image
# Sales totals by branch bar chart
branch_sales = df.groupby('Branch')['Total'].sum()
branch_sales.plot(kind='bar', color=['orange','blue','green'])
plt.ylabel('Total Sales')
plt.title('Total Sales by Branch')
plt.tight_layout()
plt.show()
No description has been provided for this image
# Product line sales breakdown pie chart
product_sales = df.groupby('Product line')['Total'].sum()
product_sales.plot(kind='pie', autopct='%1.1f%%', figsize=(6,6))
plt.title('Sales Share by Product Line')
plt.ylabel('')
plt.tight_layout()
plt.show()
No description has been provided for this image

Practice: Explore Data Yourself#

Try creating your own charts.

  • Plot sales by Customer Type or Gender.
  • Group by month instead of day.

You can use:

  • df.groupby(...)
  • .sum(), .mean(), .count()

This practice will help you get familiar with Python data tools.

# Example: Group sales by Gender
gender_sales = df.groupby('Gender')['Total'].sum()
gender_sales.plot(kind='bar', color=['pink', 'lightblue'])
plt.title('Total Sales by Gender')
plt.ylabel('Total Sales')
plt.show()
No description has been provided for this image

Next Steps: Get Ready for Streaming Dashboards!#

You have learned how to load, explore, and visualize supermarket data.

Coming up: Make interactive dashboards using Plotly and Streamlit.

Next time you will learn to build web apps from this notebook analysis.

If you enjoyed, subscribe to the channel and practice these skills with new datasets.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.