Group Part II: Data Modeling and Visualization

Q12: Data Aggregation Problem Statement: Analyzing Sales Performance by Region in a Retail Company Dataset: "Retail_Sales_Data.csv" Description: The dataset contains information about sales transactions in a retail company. It includes attributes such as transaction date, product category, quantity sold, and sales amount. The goal is to perform data aggregation to analyze the sales performance by region and identify the top-performing regions. Tasks to Perform: 1. Import the "Retail_Sales_Data.csv" dataset. 2. Explore the dataset to understand its structure and content. 3. Identify the relevant variables for aggregating sales data, such as region, sales amount, and product category. 4. Group the sales data by region and calculate the total sales amount for each region. 5. Create bar plots or pie charts to visualize the sales distribution by region. 6. Identify the top-performing regions based on the highest sales amount. 7. Group the sales data by region and product category to calculate the total sales amount for each combination. 8. Create stacked bar plots or grouped bar plots to compare the sales amounts across different regions and product categories.

Data Aggregation

Solution and implementation for Q12 from Data Modeling & Visualization (dmv).

12_retail_sales_aggregation.ipynb Download

Q12: Data Aggregation

Problem Statement: Analyzing Sales Performance by Region in a Retail Company

Code Cell [In]
import pandas as pd
import matplotlib.pyplot as plt

retail = pd.read_csv("Retail_Sales_Data.csv", parse_dates=["TransactionDate"])
print("Transactions:", len(retail))
print(retail.head().to_string(index=False))
print("Regions:", sorted(retail["Region"].unique()))
Code Cell [In]
# Total sales and transaction counts by region.
by_region = retail.groupby("Region").agg(TotalSales=("SalesAmount", "sum"), Transactions=("TransactionID", "count"))
by_region = by_region.sort_values("TotalSales", ascending=False)
print(by_region.round(2).to_string())
print("Top-performing region:", by_region.index[0])
Code Cell [In]
# Compare the regional distribution as both bars and shares.
fig, axes = plt.subplots(1, 2, figsize=(11, 4))
by_region["TotalSales"].plot.bar(ax=axes[0], title="Sales by region", color="steelblue")
axes[0].set_ylabel("Total sales")
by_region["TotalSales"].plot.pie(ax=axes[1], title="Regional sales share", autopct="%1.0f%%")
axes[1].set_ylabel("")
plt.tight_layout()
plt.show()
Code Cell [In]
# Group by both region and product category to expose the sales mix.
regional_category = retail.pivot_table(index="Region", columns="ProductCategory", values="SalesAmount", aggfunc="sum", fill_value=0)
print(regional_category.round(2).to_string())
fig, axes = plt.subplots(1, 2, figsize=(13, 4))
regional_category.plot.bar(stacked=True, ax=axes[0], title="Stacked sales mix")
regional_category.plot.bar(ax=axes[1], title="Grouped category comparison")
for axis in axes:
    axis.set_ylabel("Sales amount")
    axis.tick_params(axis="x", rotation=0)
plt.tight_layout()
plt.show()

Other Questions in Data Modeling & Visualization

See All Available Questions
Download