Mini Project: Building a Sales Report
In this project, we'll read a small sales dataset with Pandas, calculate total sales, fill in missing quantities, produce a sales summary by category, and find the best-selling product. This is the basic flow behind real-world projects like shop reports, restaurant menu sales, and online store order summaries.
import pandas as pd
import numpy as np
orders = pd.DataFrame({
"OrderID": [101, 102, 103, 104, 105],
"Category": ["Drink", "Drink", "Food", "Food", "Drink"],
"Product": ["Tea", "Coffee", "Cake", "Noodle", "Tea"],
"Price": [1200, 1800, 2500, 5000, 1200],
"Qty": [3, 2, 1, np.nan, 2]
})
# 1. Check missing values
print("Missing values:")
print(orders.isna().sum())
# 2. Fill missing Qty with 0
orders["Qty"] = orders["Qty"].fillna(0)
# 3. Create Total column
orders["Total"] = orders["Price"] * orders["Qty"]
# 4. Summarize sales by category
category_report = orders.groupby("Category")["Total"].sum().sort_values(ascending=False)
# 5. Find best-selling product by total sales
product_report = orders.groupby("Product")["Total"].sum().sort_values(ascending=False)
print("
Clean Orders:")
print(orders)
print("
Category Report:")
print(category_report)
print("
Best-selling Product:")
print(product_report.head(1))This project flow is the standard path for any data analysis task. First check for missing values, clean the data, add calculated columns, then produce a report with groupby(). You can reuse this pattern across most data projects.
Missing values: OrderID 0 Category 0 Product 0 Price 0 Qty 1 dtype: int64 Clean Orders: OrderID Category Product Price Qty Total 0 101 Drink Tea 1200 3.0 3600.0 1 102 Drink Coffee 1800 2.0 3600.0 2 103 Food Cake 2500 1.0 2500.0 3 104 Food Noodle 5000 0.0 0.0 4 105 Drink Tea 1200 2.0 2400.0 Category Report: Category Drink 9600.0 Food 2500.0 Name: Total, dtype: float64 Best-selling Product: Product Tea 6000.0 Name: Total, dtype: float64