Pandas pd.pivot_table() function
pd.pivot_table() is a function in the Pandas library used tocreate a pivot tablefunction. A pivot table is a common feature in spreadsheets that groups, aggregates, and reshapes data, making it easy to analyze data from different perspectives.
The difference between pd.crosstab() Compared topivot_table() is more powerful, supporting multiple aggregation functions, handling missing values, and more complex data reshaping.
Word meaning: pivot_table means "pivot table", a table that can dynamically change the data layout, allowing summary analysis from multiple dimensions.
Basic syntax and parameters
pd.pivot_table() is a top-level function of the Pandas library used to create pivot tables.
Syntax format
pd.pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All')
Parameter Description
- Parameters:
data - Type: DataFrame.
- Description: the source DataFrame.
- Parameters:
values - Type: a column name or a list of column names.
- Description: the column(s) to aggregate. If not specified, all numeric columns are aggregated.
- Parameters:
index - Type: a column name, a list of column names, or a function.
- Description: the column used as the row index. It can be a single column or multiple columns (a list); multiple columns create a hierarchical index.
- Parameters:
columns - Type: a column name or a list of column names.
- Description: the column(s) used as the column index.
- Parameters:
aggfunc - Type: function, string, or list of functions.
- Description: the aggregation function. Common values include 'sum', 'mean', 'count', 'min', 'max', 'median', 'std', etc. The default is 'mean'.
- Parameters:
fill_value - Type: scalar or None.
- Description: the value used to fill missing values.
- Parameters:
margins - Type: boolean.
- Description: whether to add row and column totals. The default is
False。
Function description
- Return Value: returns a DataFrame, namely the pivot table.
- effect: reshapes and summarizes data according to the specified rows, columns, and aggregation function.
Example
Let us thoroughly master pd.pivot_table() through a series of examples from simple to complex.
Example 1: Basic usage - Creating a simple pivot table
Example
import
pandas
as
pd
import
numpy
as
np
1. Create sales data
sales
=
pd.
DataFrame
(
{
'date'
: pd.
date_range
(
'2023-01-01'
,
periods
=
12
,
freq
=
'D'
)
,
'product'
:
[
'A'
,
'B'
,
'C'
,
'A'
,
'B'
,
'C'
,
'A'
,
'B'
,
'C'
,
'A'
,
'B'
,
'C'
]
,
'region'
:
[
'North'
,
'North'
,
'North'
,
'South'
,
'South'
,
'South'
,
'East'
,
'East'
,
'East'
,
'West'
,
'West'
,
'West'
]
,
'sales'
:
[
100
,
150
,
200
,
180
,
170
,
160
,
190
,
200
,
210
,
220
,
230
,
240
]
}
)
(
"=== Sales data ==="
)
(
sales
)
2. Create a pivot table - summarize sales by product and region
pivot
=
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
'product'
,
columns
=
'region'
,
aggfunc
=
'sum'
)
(
"n=== pd.pivot_table() sales pivot table ==="
)
(
pivot
)
3. Use fill_value to fill missing values
(
"n=== fill missing values (fill_value=0) ==="
)
pivot_fill
=
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
'product'
,
columns
=
'region'
,
aggfunc
=
'sum'
,
fill_value
=
0
)
(
pivot_fill
)
Expected output:
=== 销售数据 ===
date product region sales
0 2023-01-01 A North 100
1 2023-01-01 B North 150
2 2023-01-01 C North 200
3 2023-01-01 A South 180
4 2023-01-01 B South 170
5 2023-01-01 C South 160
6 2023-01-01 A East 190
7 2023-01-01 B East 200
8 2023-01-01 C East 210
9 2023-01-01 A West 220
10 2023-01-01 B West 230
11 2023-01-01 C West 240
=== pd.pivot_table() 销售透视表 ===
region East North South West
product
A 400.0 100.0 180.0 220.0
B 400.0 150.0 170.0 230.0
C 410.0 200.0 160.0 240.0
=== 填充缺失值 (fill_value=0) ===
region East North South West
product
A 400 100 180 220
B 400 150 170 230
C 410 200 160 240
Code analysis:
- The pivot table summarizes sales by product (rows) and region (columns).
fill_value=0Missing combinations are shown as 0 for easier reading.
Example 2: Using different aggregation functions
aggfunc The parameter can specify multiple aggregation methods to analyze data.
Example
import
pandas
as
pd
import
numpy
as
np
1. Create richer sales data
sales
=
pd.
DataFrame
(
{
'product'
:
[
'A'
,
'B'
,
'A'
,
'B'
,
'A'
,
'B'
,
'A'
,
'B'
,
'A'
,
'B'
]
,
'region'
:
[
'North'
,
'North'
,
'South'
,
'South'
,
'East'
,
'East'
,
'West'
,
'West'
,
'North'
,
'South'
]
,
'sales'
:
[
100
,
150
,
200
,
180
,
190
,
200
,
220
,
230
,
110
,
190
]
,
'quantity'
:
[
10
,
15
,
20
,
18
,
19
,
20
,
22
,
23
,
11
,
19
]
}
)
(
"=== Sales data ==="
)
(
sales
)
2. Aggregate (sum)
(
"n=== sales sum ==="
)
sum_result
=
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
'product'
,
columns
=
'region'
,
aggfunc
=
'sum'
)
(
sum_result
)
3. Calculate the average sales
(
"n=== Average sales ==="
)
mean_result
=
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
'product'
,
columns
=
'region'
,
aggfunc
=
'mean'
)
(
mean_result.
round
(
1
)
)
4. Calculate multiple statistics at once (pass a list)
(
"n=== compute count, sum, and mean together ==="
)
multi_agg
=
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
'product'
,
columns
=
'region'
,
aggfunc
=
[
'sum'
,
'mean'
]
)
(
multi_agg
)
Expected output:
=== 销售数据 ===
product region sales quantity
0 A North 100 10
1 B North 150 15
2 A South 200 20
3 B South 180 18
4 A East 190 19
5 B East 200 20
6 A West 220 22
7 B West 230 23
8 A North 110 11
9 B South 190 19
=== 销售额求和 ===
region East North South West
product
A 190 210 200 220
B 200 150 370 230
=== 销售额平均值 ===
region East North South West
product
A 190 105.0 200 220
B 200 150.0 185 230
=== 同时计算数量和、均值 ===
sum mean
region East North South West East North South West
product
A 190 210 200 220 190 105.0 200 220
B 200 150 370 230 200 150.0 185.0 230
Code analysis:
- Different aggregation functions can compute different statistical measures.
- When multiple aggregation functions are passed at once, the pivot table creates a multi-level column index.
Example 3: Using margins to add totals
Use margins The parameter can add row and column totals, making it easy to view summary data.
Example
import
pandas
as
pd
Use the previous sales data
sales
=
pd.
DataFrame
(
{
'product'
:
[
'A'
,
'B'
,
'A'
,
'B'
,
'A'
,
'B'
]
,
'region'
:
[
'North'
,
'North'
,
'South'
,
'South'
,
'East'
,
'East'
]
,
'sales'
:
[
100
,
150
,
200
,
180
,
190
,
200
]
}
)
1. No totals
(
"=== No totals ==="
)
(
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
'product'
,
columns
=
'region'
,
aggfunc
=
'sum'
)
)
2. Add row and column totals
(
"n=== Add totals (margins=True) ==="
)
result
=
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
'product'
,
columns
=
'region'
,
aggfunc
=
'sum'
,
margins
=
True
)
(
result
)
3. Customize the total name
(
"n=== Custom total name ==="
)
result_name
=
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
'product'
,
columns
=
'region'
,
aggfunc
=
'sum'
,
margins
=
True
,
margins_name
=
'Total'
)
(
result_name
)
4. Totals with a multi-level row index
(
"n=== Multi-level row index ==="
)
multi_index
=
pd.
pivot_table
(
sales
,
values
=
'sales'
,
index
=
[
'product'
,
'region'
]
,
aggfunc
=
'sum'
)
(
multi_index
)
Expected output:
=== 无合计 ===
region East North South
product
A 190 100 200
B 200 150 180
=== 添加合计 (margins=True) ===
region East North South All
product
A 190 100 200 490
B 200 150 180 530
All 390 250 380 1020
=== 自定义合计名称 ===
region East North South 总计
product
A 190 100 200 490
B 200 150 180 530
总计 390 250 380 1020
=== 多层行索引 ===
sales
product region
A East 190
North 100
South 200
B East 200
North 150
South 180
Code analysis:
margins=Trueadds "All" rows and columns showing the total data.margins_nameYou can customize the name of the totals.
Example 4: multi-level indexes and complex pivots
You can use multiple columns to create a pivot table with a multi-level index.
Example
import
pandas
as
pd
1. Create data containing multiple categorical variables
data
=
pd.
DataFrame
(
{
'year'
:
[
2022
,
2022
,
2022
,
2023
,
2023
,
2023
]
,
'quarter'
:
[
'Q1'
,
'Q2'
,
'Q3'
,
'Q1'
,
'Q2'
,
'Q3'
]
,
'product'
:
[
'A'
,
'A'
,
'B'
,
'B'
,
'A'
,
'B'
]
,
'sales'
:
[
100
,
150
,
200
,
220
,
180
,
250
]
}
)
(
"=== data ==="
)
(
data
)
2. Use multiple columns as the row index
(
"n=== pivot across multiple years and quarters ==="
)
pivot
=
pd.
pivot_table
(
data
,
values
=
'sales'
,
index
=
[
'year'
,
'quarter'
]
,
columns
=
'product'
,
aggfunc
=
'sum'
)
(
pivot
)
3. Use multiple columns as the column index
(
"n=== complex cross pivot ==="
)
pivot_multi
=
pd.
pivot_table
(
data
,
values
=
'sales'
,
index
=
'year'
,
columns
=
[
'product'
,
'quarter'
]
,
aggfunc
=
'sum'
)
(
pivot_multi
)
4. Use fill_value to handle missing values
(
"n=== fill missing values ==="
)
pivot_fill
=
pd.
pivot_table
(
data
,
values
=
'sales'
,
index
=
[
'year'
,
'quarter'
]
,
columns
=
'product'
,
aggfunc
=
'sum'
,
fill_value
=
0
)
(
pivot_fill
)
Expected output:
=== 数据 ===
year quarter product sales
0 2022 Q1 A 100
1 2022 Q2 A 150
2 2022 Q3 B 200
3 2023 Q1 B 220
4 2023 Q2 A 180
5 2023 Q3 B 250
=== 多年份多季度透视 ===
product A B
year quarter
2022 Q1 100.0 NaN
2022 Q2 150.0 NaN
2022 Q3 NaN 200.0
2023 Q1 NaN 220.0
2023 Q2 180.0 NaN
2023 Q3 NaN 250.0
=== 复杂交叉透视 ===
product A B
quarter Q1 Q2 Q3 Q1 Q2 Q3
year
2022 100 150 200 NaN NaN NaN
2023 NaN 180 250 NaN NaN NaN
=== 填充缺失值 ===
product A B
year quarter
2022 Q1 100 0
2022 Q2 150 0
2022 Q3 0 200
2023 Q1 0 220
2023 Q2 180 0
2023 Q3 0 250
Code analysis:
- A multi-level index is specified as a list
index=['year', 'quarter']implementation based on the dynamic type of the object. fill_value=0Missing data is uniformly filled with 0 for easier subsequent calculations.
Tip:
pd.pivot_table()provides powerful pivot functionality. If you only need to count the frequency of two categorical variables,pd.crosstab()might be simpler.pd.crosstab()returns frequency counts, whilepivot_table()can compute a variety of statistics such as the mean, sum, and standard deviation.pd.crosstab()can be regarded aspivot_table()inaggfunc='count'a special case of
Common Pandas functions