# 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.

## Find more … …

Excel Data Analysis for Beginner and Data Analyst : Tutorial 15 – Pivot Table

Excel Data Analysis for Beginner and Data Analyst : Tutorial 30 – Power Pivot

Excel formula for Beginners – How to Count if two criteria match in Excel

### Essential Gigs

**Nilimesh: I will develop time series forecasting model for you using python or r for $50 on…**

*For only $50, Nilimesh will develop time series forecasting model for you using python or r. | Note: please contact me…*www.fiverr.com

**Nilimesh: I will do your data analytics and econometrics projects in python for $50 on fiverr.com**

*For only $50, Nilimesh will do your data analytics and econometrics projects in python. | Note: please contact me…*www.fiverr.com

**Nilimesh: I will do your machine learning and data science projects in python for $50 on fiverr.com**

*For only $50, Nilimesh will do your machine learning and data science projects in python. | Note: please contact me…*www.fiverr.com

**Nilimesh: I will do your gis and spatial programming projects in python for $50 on fiverr.com**

*For only $50, Nilimesh will do your gis and spatial programming projects in python. | Note: please contact me before…*www.fiverr.com