w3resource

Pandas Pivot Table: Create a Pivot table and find the region wise, item wise unit sold

Pandas: Pivot Table Exercise-6 with Solution

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:


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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-6.php