Pandas Pivot Table: Create a Pivot table and find manager wise, salesman wise total sale and also display the sum of all sale amount at the bottom
Pandas: Pivot Table Exercise-8 with Solution
Write a Pandas program to create a Pivot table and find manager wise, salesman wise total sale and also display the sum of all sale amount at the bottom. Go to Excel data
Sample Solution:
Python Code :
import pandas as pd
import numpy as np
df = pd.read_excel('E:\SaleData.xlsx')
table = pd.pivot_table(df,index=["Manager","SalesMan"],values=["Units","Sale_amt"],
aggfunc=[np.sum],fill_value=0,margins=True)
print(table)
Sample Output:
sum Sale_amt Units Manager SalesMan Douglas John 124016.0 156 Karen 48204.0 170 Michael 66836.0 89 Hermann Luis 206373.0 281 Shelli 33698.0 193 Sigal 125037.5 173 Martha Alexander 236703.0 396 Diana 36100.0 125 Steven 199690.0 183 Timothy David 140955.0 213 Stephen 88063.0 142 All 1305675.5 2121
Pivot Table:
Salesdata.xlsx:
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 count the manager wise sale and mean value of sale amount.
Next: Write a Pandas program to create a Pivot table and find the total sale amount region wise, manager wise, sales man wise where Manager = "Douglas".
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/pandas/excel/pandas-pivot-exercise-8.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics