Note

Description:

You are a sales analyst for an e-commerce company. You have been tasked with analyzing the sales data to determine which regions and product categories are generating the most revenue. You have a dataset containing information about sales, including the region, product category, and sales amount.

Tasks:

Using Pandas, write a program to:

  • Load the dataset into a pandas DataFrame.
  • Group the sales data by region and product category, and calculate the total sales amount for each region and product category.
  • Calculate the total sales amount for each region and product category as a percentage of the overall sales amount.
  • Print the top 3 regions and product categories with their total sales amount and percentage of overall sales.

Hint: You can use the groupby function to group the data by region and product category, and calculate the total sales amount for each group. Then, use the pivot_table function to reshape the data and calculate the percentages.

Here is the dataset

Region,Product Category,Sales Amount
North,Electronics,1000
North,Clothing,800
South,Electronics,1200
South,Electronics,900
East,Clothing,1100
East,Electronics,700
West,Electronics,1000
West,Electronics,900
North,Clothing,600
South,Clothing,1000
import pandas as pd
import sys
print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
Python version 3.11.7 
Pandas version 2.2.1
df = pd.read_clipboard(sep=",")
df
RegionProduct CategorySales Amount
0NorthElectronics1000
1NorthClothing800
2SouthElectronics1200
3SouthElectronics900
4EastClothing1100
5EastElectronics700
6WestElectronics1000
7WestElectronics900
8NorthClothing600
9SouthClothing1000
# group the sales data by region and product category
group = df.groupby(['Region','Product Category'])
 
# total sales of group
group.sum()
Sales Amount
RegionProduct Category
EastClothing1100
Electronics700
NorthClothing1400
Electronics1000
SouthClothing1000
Electronics2100
WestElectronics1900
# flatten the group
agg = group['Sales Amount'].sum().reset_index()
agg
RegionProduct CategorySales Amount
0EastClothing1100
1EastElectronics700
2NorthClothing1400
3NorthElectronics1000
4SouthClothing1000
5SouthElectronics2100
6WestElectronics1900
# calculate total sales amount
total_sales = agg['Sales Amount'].sum()
 
# calculate percentage of total sales for each region and product category
agg['Percentage of Total Sales'] = (agg['Sales Amount'] / total_sales) * 100
agg
RegionProduct CategorySales AmountPercentage of Total Sales
0EastClothing110011.956522
1EastElectronics7007.608696
2NorthClothing140015.217391
3NorthElectronics100010.869565
4SouthClothing100010.869565
5SouthElectronics210022.826087
6WestElectronics190020.652174
# get the top 3 regions
agg.sort_values(by='Percentage of Total Sales', ascending=False).head(3)
RegionProduct CategorySales AmountPercentage of Total Sales
5SouthElectronics210022.826087
6WestElectronics190020.652174
2NorthClothing140015.217391

Here is an alternative solution using pivot tables.

# pivot the data to get sales amount by region and product category
pivot = pd.pivot_table(df, values='Sales Amount', index='Region', columns='Product Category', aggfunc='sum')
pivot
Product CategoryClothingElectronics
Region
East1100.0700.0
North1400.01000.0
South1000.02100.0
WestNaN1900.0
# calculate total sales amount
total_sales = df['Sales Amount'].sum()
total_sales
9200
# calculate percentage of total sales for each region and product category
agg = (pivot / total_sales) * 100
agg
Product CategoryClothingElectronics
Region
East11.9565227.608696
North15.21739110.869565
South10.86956522.826087
WestNaN20.652174

With the shape of this DataFrame ordering by percentage of total sales is not really possible.

# the stack function lets us reshape the data so we can sort by the percentages
agg.stack().sort_values(ascending=False).head(3)
Region  Product Category
South   Electronics         22.826087
West    Electronics         20.652174
North   Clothing            15.217391
dtype: float64

Summary:

Now that you’ve gone through the tutorial, you’ve seen how the sales data was analyzed to find the best-selling regions and product categories. The analyst first loaded the data into a Pandas DataFrame and then grouped it by region and product category to calculate total sales for each combination. They then calculated the percentage of total sales for each group, which made it easy to compare performance across different regions and categories. Finally, they sorted the results to identify the top 3 regions and product categories, giving a clear picture of where the company’s sales were strongest.

Key Takeaways:

  • Data Loading: Use pd.read_clipboard() to load data from clipboard into a DataFrame.
  • Data Grouping: Utilize df.groupby() to group data by multiple columns (region and product category).
  • Aggregation: Apply sum() to calculate total sales for each group.
  • Data Reshaping: Employ reset_index() to flatten grouped data and pd.pivot_table() for alternative data representation.
  • Percentage Calculation: Calculate percentages of total sales using (grouped_sum / total_sales) * 100.
  • Sorting and Ranking: Use sort_values() and head() to identify top-performing regions and product categories.
  • Data Transformation: Apply stack() to reshape pivoted data for easier sorting.