w3resource

Pandas Pivot Table: Create a Pivot table and find the total sale amount region wise, manager wise, sales man wise


3. Region, Manager, and Salesman-Wise Total Sale Amount

Write a Pandas program to create a Pivot table and find the total sale amount region wise, manager wise, sales man wise. 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","Manager","SalesMan"], values="Sale_amt", aggfunc=np.sum))

Sample Output:

                           Sale_amt
Region  Manager SalesMan           
Central Douglas John       124016.0
        Hermann Luis       206373.0
                Shelli      33698.0
                Sigal      125037.5
        Martha  Steven     199690.0
        Timothy David      140955.0
East    Douglas Karen       48204.0
        Martha  Alexander  236703.0
                Diana       36100.0
West    Douglas Michael     66836.0
        Timothy Stephen     88063.0                                       

Pivot Table:

Salesdata.xlsx:



For more Practice: Solve these Related Problems:

  • Write a Pandas program to create a pivot table that calculates total sale amount by region, manager, and salesman.
  • Write a Pandas program to generate a three-dimensional pivot table using region, manager, and salesman as indexes.
  • Write a Pandas program to build a pivot table with region, manager, and salesman levels, and then verify the aggregated sale amount using sum.
  • Write a Pandas program to create a pivot table from Salesdata.xlsx with three indexes (region, manager, salesman) and display the resulting multi-index structure.

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 total sale amount region wise, manager wise.
Next: Write a Pandas program to create a Pivot table and find the item wise unit sold.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.