Pandas DataFrame.pivot_table() method: Explained with examples

Updated: February 20, 2024 By: Guest Contributor Post a comment


The Pandas pivot_table() method is a powerful tool for reshaping, summarizing, and analyzing data in Python’s Pandas library. Whether you are dealing with sales data, survey results, or any other form of tabular data, pivot_table() can help you gain insights by reorganizing your data’s structure, allowing for quick and efficient analyses.

Getting Started

First, let’s understand what a Pivot Table is. A pivot table is a data summarization tool that is commonly used in the context of data processing. It aggregates a table of data by one or more keys, arranging the data in a rectangle with some of the group keys along the rows and some along the columns.

Before diving into examples, you’ll need to ensure you have Pandas installed in your Python environment. You can install Pandas using pip:

pip install pandas

Basic Usage

Now, let’s dive straight into examples, starting from basic to more advanced uses of the pivot_table() method.

Example 1: Creating a Basic Pivot Table

Suppose you have the following DataFrame:

import pandas as pd

# Sample DataFrame
data = {'Product': ['Apples', 'Bananas', 'Oranges', 'Apples', 'Bananas', 'Oranges'],
        'Sales': [20, 30, 40, 15, 25, 35],
        'City': ['New York', 'Los Angeles', 'New York', 'Los Angeles', 'New York', 'Los Angeles']}
df = pd.DataFrame(data)

To create a pivot table that summarizes the sales by product, you can use:

pivot_df = df.pivot_table(index='Product', values='Sales', aggfunc='sum')

This simple pivot table aggregates sales for each product. The output will be:

Apples        35
Bananas       55
Oranges       75

Example 2: Adding More Dimensions

To include more dimensions, such as summarizing sales by both product and city, you modify your pivot like this:

pivot_df = df.pivot_table(index=['Product', 'City'], values='Sales', aggfunc='sum')

The output shows sales aggregated not just by product, but also by city:

Product City            
Apples  Los Angeles    20
        New York       15
Bananas Los Angeles    30
        New York       25
Oranges Los Angeles    35
        New York       40

Advanced Usage

Example 3: Using Different Aggregation Functions

The pivot_table() allows for the use of multiple aggregation functions, which you can pass as a list to the aggfunc parameter. For example, to find the sum and average sales by product:

pivot_df = df.pivot_table(index='Product', values='Sales', aggfunc=['sum', 'mean'])

The result is a pivot table that provides both the total and average sales for each product:

               sum  mean
          Sales Sales
Apples        35  17.5
Bananas       55  27.5
Oranges       75  37.5

Example 4: Including Margins

By specifying margins=True, Pandas can append all partial totals. This is especially useful for providing a grand total across all dimensions. Here’s how to do it:

pivot_df = df.pivot_table(index='Product', values='Sales', aggfunc='sum', margins=True)

And the output will now include a grand total for Sales:

Apples        35
Bananas       55
Oranges       75
All          165


The pivot_table() method in Pandas is a versatile feature that simplifies the process of reshaping, summarizing, and analyzing your data. By understanding its various parameters and capabilities, you can unlock powerful insights from your datasets with minimal effort. The examples provided here barely scratch the surface of what’s possible with pivot tables in Pandas, encouraging further exploration and experimentation to fully leverage this powerful tool.