How to create PIVOT table using Pandas DataFrame in Python

Hits: 445

How to create PIVOT table using Pandas DataFrame in Python

A pivot table is a powerful tool that can be used to organize and summarize data in a Pandas DataFrame. Creating a pivot table using a Pandas DataFrame in Python is easy and can be done using the pivot_table() function.

First, you need to import the Pandas library and create a DataFrame. For example, you can create a DataFrame with some sample data.

import pandas as pd

data = {'product': ['A', 'B', 'C', 'D'], 
        'location': ['X', 'Y', 'X', 'Y'], 
        'sales': [100, 120, 90, 110]
       }

df = pd.DataFrame(data)

Next, you can use the pivot_table() function to create a pivot table. This function takes several parameters:

  • values: This is the column you want to use to calculate the values in the pivot table.
  • index: This is the column you want to use as the index of the pivot table.
  • columns: This is the column you want to use as the columns of the pivot table.
  • aggfunc: This is the function you want to use to calculate the values in the pivot table.

For example, to create a pivot table that shows the total sales by product and location, you can use the following code:

pivot_table = df.pivot_table(values='sales', 
                             index='product', 
                             columns='location', 
                             aggfunc='sum')

The resulting pivot table will have product as the index and location as the columns, and the values will be the sum of the sales.

 

Alternatively, you can also use the pivot() function to create a pivot table, it takes the same parameters like index,column and values, but doesn’t take aggfunc parameter.

You can also use other functions like mean, median, count etc as the aggfunc parameter to get the respective values in pivot table.

By using the pivot_table() function, you can easily create a pivot table that organizes and summarizes your data in a useful way. This can be a powerful tool for data analysis and can help you to quickly understand and make decisions based on your data.

In this Learn through Codes example, you will learn: How to create PIVOT table using Pandas DataFrame in Python.



 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners