Python Central HubSign in

Data Analytics with Python / Phase 10: Data Analytics Projects

E-commerce Sales Analysis

Analyze sales data - revenue trends, top products, cohorts by month, and basic KPIs.

Goal

Given an e-commerce orders dataset, answer:

  • Overall revenue and order trends
  • Top products / categories
  • Average order value (AOV)
  • Revenue by segment (city, category)

Analysis pipeline

diagram E-commerce sales pipeline mermaid
From raw orders to the revenue drivers behind them.

Example columns

  • order_id, order_date, customer_id
  • product, category, qty, price
  • city, status

Step 1: Load and basic prep

Python
import pandas as pd
 
df = pd.read_csv("data/orders.csv")
 
df["order_date"] = pd.to_datetime(df["order_date"], errors="coerce")
 
df["revenue"] = df["qty"] * df["price"]
 
print(df.shape)
print(df.head())

Step 2: KPIs

Python
orders = df["order_id"].nunique()
customers = df["customer_id"].nunique()
revenue = df["revenue"].sum()
aov = revenue / orders
 
print("orders:", orders)
print("customers:", customers)
print("revenue:", revenue)
print("aov:", aov)

Step 3: Revenue trend

Python
import matplotlib.pyplot as plt
 
monthly = (
    df.dropna(subset=["order_date"])
      .groupby(df["order_date"].dt.to_period("M"))
      .agg(revenue=("revenue", "sum"), orders=("order_id", "nunique"))
      .reset_index()
)
 
monthly["order_date"] = monthly["order_date"].astype(str)
 
plt.figure(figsize=(10, 4))
plt.plot(monthly["order_date"], monthly["revenue"], marker="o")
plt.title("Monthly revenue")
plt.xticks(rotation=30)
plt.tight_layout()
plt.show()

Step 4: Top products

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
top = (
    df.groupby("product")
      .agg(revenue=("revenue", "sum"))
      .sort_values("revenue", ascending=False)
      .head(10)
      .reset_index()
)
 
plt.figure(figsize=(10, 4))
sns.barplot(data=top, x="revenue", y="product")
plt.title("Top 10 products by revenue")
plt.tight_layout()
plt.show()

Visualize it

sketch Monthly revenue trend p5.js
A rising line means revenue is growing month over month; dips are worth investigating.

Deliverable

Summarize:

  • Revenue trend (up/down)
  • Main revenue drivers (top products/categories)
  • Any anomalies (returns, cancellations)

Check your understanding

quizCheck yourself
  1. How is the revenue column derived?

    Show answer

    A โ€” qty * price โ€” Each row has quantity and unit price, so their product is the line revenue. Every KPI then builds on it.

  2. How is average order value (AOV) computed in the KPI step?

    Show answer

    A โ€” Total revenue divided by the number of unique orders โ€” The code uses revenue / df["order_id"].nunique(), counting orders once even with several line items.

  3. Why use nunique on order_id instead of len(df)?

    Show answer

    A โ€” An order can have several rows, so counting rows would overcount orders โ€” Each row may be one product line, so unique order IDs give the true order count.

  4. How are monthly revenue figures built?

    Show answer

    A โ€” Group by order_date.dt.to_period("M") and sum revenue โ€” Converting dates to monthly periods and aggregating gives a monthly trend, after dropping rows with bad dates.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Compute revenue per order

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Average order value (AOV)

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Top products by revenue

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did