How to Create a Pivot Table in Python using Pandas?
Pivot table is a statistical table that summarizes a substantial table like big datasets. It is part of data processing. This summary in pivot tables may include mean, median, sum, or other statistical terms. Pivot tables are originally associated with MS Excel but we can create a pivot table in Python using Pandas using the dataframe.pivot() method.
Syntax : dataframe.pivot(self, index=None, columns=None, values=None, aggfunc)
index: Column for making new frame’s index.
columns: Column for new frame’s columns.
values: Column(s) for populating new frame’s values.
aggfunc: function, list of functions, dict, default numpy.mean
Let’s first create a dataframe that includes Sales of Fruits.
Get the total sales of each product
Get the total sales of each category
Get the total sales of by category and product both
Get the Mean, Median, Minimum sale by category
Get the Mean, Median, Minimum sale by product
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course