Pandas Pivot Table: Create a Pivot table and find the region wise, item wise unit sold
6. Region and Item-Wise Units Sold
Write a Pandas program to create a Pivot table and find the region wise, item wise unit sold. Go to Excel data
Sample Solution:
Python Code :
import numpy as np
import pandas as pd
df = pd.read_excel('E:\SaleData.xlsx')
print(pd.pivot_table(df,index=["Region", "Item"], values="Units", aggfunc=np.sum))
Sample Output:
Units Region Item Central Cell Phone 27.0 Desk 7.0 Home Theater 424.0 Television 498.0 Video Games 243.0 East Cell Phone 175.0 Home Theater 234.0 Television 130.0 Video Games 152.0 West Cell Phone 76.0 Desk 3.0 Home Theater 64.0 Television 88.0
Pivot Table:
Salesdata.xlsx:
For more Practice: Solve these Related Problems:
- Write a Pandas program to create a pivot table with region and item as indexes and compute the total units sold.
- Write a Pandas program to generate a pivot table that aggregates unit sales by region and item, then sort by region.
- Write a Pandas program to build a pivot table from Salesdata.xlsx that groups by region and item to sum the units sold.
- Write a Pandas program to create a pivot table that displays item-wise unit sales for each region and then filter for regions with high unit sales.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to create a Pivot table and find the region wise total sale.
Next: Write a Pandas program to create a Pivot table and count the manager wise sale and mean value of sale amount.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.