w3resource

Pandas Pivot Table: Create a Pivot table and find the region wise Television and Home Theater sold


10. Region-Wise Television and Home Theater Sold

Write a Pandas program to create a Pivot table and find the region wise Television and Home Theater sold. Go to Excel data

Sample Solution:

Python Code :


import pandas as pd
df = pd.read_excel('E:\SaleData.xlsx')
table = pd.pivot_table(df,index=["Region", "Item"], values="Units", aggfunc=["sum"])
print(table.query('Item == ["Television","Home Theater"]'))

Sample Output:

                          Units
Region  Item                   
Central Home Theater  53.000000
        Television    55.333333
East    Home Theater  46.800000
        Television    65.000000
West    Home Theater  32.000000
        Television    44.000000                                       

Pivot Table:

Salesdata.xlsx:



For more Practice: Solve these Related Problems:

  • Write a Pandas program to create a pivot table that aggregates units sold for Television and Home Theater by region.
  • Write a Pandas program to generate a pivot table from Salesdata.xlsx that filters data for Television and Home Theater items and sums their sales region-wise.
  • Write a Pandas program to build a pivot table that groups by region and item, then display only the rows corresponding to Television and Home Theater.
  • Write a Pandas program to create a pivot table to compare the sale of Television and Home Theater across different regions.

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, sales man wise.
Next: Write a Pandas program to create a Pivot table and find the maximum sale value of the items.

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.