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
flowchart LR A["Raw orders
(CSV)"] --> B["Clean
(parse dates, revenue = qty * price)"] B --> C["KPIs
(orders, AOV)"] C --> D["Visualize
(monthly trend, top products)"] D --> E["Conclude
(main revenue drivers)"]
Example columns
order_id,order_date,customer_idproduct,category,qty,pricecity,status
Step 1: Load and basic prep
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
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
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
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
Deliverable
Summarize:
- Revenue trend (up/down)
- Main revenue drivers (top products/categories)
- Any anomalies (returns, cancellations)
Check your understanding
How is the revenue column derived?
Each row has quantity and unit price, so their product is the line revenue. Every KPI then builds on it.
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.
How is average order value (AOV) computed in the KPI step?
The code uses revenue / df["order_id"].nunique(), counting orders once even with several line items.
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.
Why use nunique on order_id instead of len(df)?
Each row may be one product line, so unique order IDs give the true order count.
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.
How are monthly revenue figures built?
Converting dates to monthly periods and aggregating gives a monthly trend, after dropping rows with bad dates.
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
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Average order value (AOV)
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ Top products by revenue
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.