pivot_table() helps you quickly summarize data by grouping and calculating statistics like sums or averages.
0
0
pivot_table() for summarization in Pandas
Introduction
You want to see total sales by product and region.
You need the average score of students by class and subject.
You want to count how many times each category appears in your data.
You want to compare data across two or more categories easily.
Syntax
Pandas
pandas.pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None)
data: your DataFrame.
values: column(s) to aggregate.
index: rows to group by.
columns: columns to group by.
aggfunc: function like 'sum', 'mean', or 'count'.
Examples
Sum sales for each product.
Pandas
pd.pivot_table(df, values='Sales', index='Product', aggfunc='sum')
Average score by class and subject.
Pandas
pd.pivot_table(df, values='Score', index='Class', columns='Subject', aggfunc='mean')
Count how many rows per category.
Pandas
pd.pivot_table(df, index='Category', aggfunc='size')
Sample Program
This code creates a table showing total sales for each product split by region. Missing values are filled with 0.
Pandas
import pandas as pd data = { 'Product': ['Apple', 'Apple', 'Banana', 'Banana', 'Carrot', 'Carrot'], 'Region': ['East', 'West', 'East', 'West', 'East', 'West'], 'Sales': [100, 150, 200, 250, 300, 350] } df = pd.DataFrame(data) pivot = pd.pivot_table(df, values='Sales', index='Product', columns='Region', aggfunc='sum', fill_value=0) print(pivot)
OutputSuccess
Important Notes
Use fill_value to replace missing values with a number like 0.
You can use multiple aggregation functions by passing a list to aggfunc.
pivot_table returns a DataFrame that is easy to read and analyze.
Summary
pivot_table() groups data by rows and columns and calculates summary stats.
It is useful for quick data summaries like sums, averages, or counts.
You can customize grouping and aggregation easily with its parameters.