7. Pivot Tables

file-download
75MB
  1. Find the total sales amount for each year.

chevron-rightSolutionhashtag
import pandas as pd

df=pd.read_csv('retail_data.csv')
ndf = df.pivot_table(values="Total_Amount", index="Year", aggfunc="sum").reset_index()
print(ndf)

# Output
    Year  Total_Amount
0  2023  3.358360e+08
1  2024  6.614229e+07

  1. How many transactions were made for each unique total purchase count, categorized by customer segments?

chevron-rightSolutionhashtag
ndf=df.pivot_table(index='Total_Purchases',values='Customer_Segment',aggfunc='count')
ndf.reset_index()

# Output
Total_Purchases	Customer_Segment
0	1	31050
1	2	31084
2	3	31070
3	4	30795
4	5	31112
5	6	27767
6	7	27696
7	8	27966
8	9	27757
9	10	27614
  1. Find the average purchase amount for each product category.

chevron-rightSolutionhashtag
ndf=df.pivot_table(index="Product_Category", values='Amount',aggfunc='mean')
ndf.reset_index()

# Output
	Product_Category	Amount
0	Books	255.072599
1	Clothing	254.907741
2	Electronics	255.408202
3	Grocery	255.350918
4	Home Decor	254.904483
  1. Find the number of transactions for each country.

chevron-rightSolutionhashtag

  1. Find the maximum payment for each payment method.

chevron-rightSolutionhashtag
  1. Find the minimum purchase amount of each product.

chevron-rightSolutionhashtag
  1. Find the total purchases made in each month across years.

chevron-rightSolutionhashtag
  1. Find the most expensive purchase amount for each product type.

chevron-rightSolutionhashtag
  1. Find the average revenue from each city and segment.

chevron-rightSolutionhashtag
  1. Find the minimum revenue for each product brand and order status.

chevron-rightSolutionhashtag

Last updated