Course contentsShow
← Python
Lesson 952 of 1,06015. Data Science FundamentalsFree lesson

Pivoting DataFrames for Summary Tables

Create intuitive summary tables by rearranging data based on unique index values and column labels to reveal hidden patterns.

In this lesson, you will learn how to transform "long" observational data into intuitive "wide" summary tables that highlight relationships between categorical variables.

The Concept of Pivoting

When working with raw datasets, your information is often stored in a "long" format—each row representing a single observation. While great for storage, these tables are difficult for humans to read when you want to compare categories across dimensions. Pivoting allows you to take one column to define your rows (the index), another to define your new columns, and a third to populate the values in the grid.

Think of it as rotating your data to provide a "bird's-eye view." For instance, if you have a dataset of sales records with columns ['Date', 'Region', 'Revenue'], a pivot table allows you to see all regions as columns and dates as rows, instantly revealing which region performed best on any given day.

Creating a Pivot Table

In pandas, the pivot() method is designed for this exact restructuring. It requires three main arguments: index (what labels your rows), columns (what spreads across the top), and values (what fills the cells).

import pandas as pd

# Long format data
data = {
    'Date': ['2023-01-01', '2023-01-01', '2023-01-02', '2023-01-02'],
    'Region': ['North', 'South', 'North', 'South'],
    'Revenue': [100, 150, 120, 130]
}
df = pd.DataFrame(data)

# Pivoting to create a summary table
summary = df.pivot(index='Date', columns='Region', values='Revenue')

print(summary)

In the output, your Date becomes the row index, North and South become the column headers, and the Revenue values fill the intersection of those two categories. If your data contains multiple entries for a single index/column combination (e.g., multiple sales in the North on Jan 1st), you would use pivot_table(), which allows you to define an aggregation function (like sum or mean) to condense those values into a single summary statistic.

By rearranging your data this way, you turn a list of individual events into a clean, analytical matrix that makes hidden patterns—like regional performance trends—immediately visible.

Key Takeaway: Pivoting restructures your data into a cross-tabulated grid, transforming raw, long-format logs into actionable, readable summary tables.