Pandas pd.pivot_table() function

Pandas 常用函数Pandas common functions


pd.pivot_table()is a function in the Pandas library used tocreate pivot tables. A pivot table is a common feature in spreadsheets that can group, aggregate, and reshape data, making it easy to analyze data from different perspectives.

andpd.crosstab()Compared withpivot_table(), it is more powerful, supporting multiple aggregation functions, handling missing values, and more complex data reshaping.

Word definition: pivot_tablemeans "pivot table", which is a table that can dynamically change the data layout and summarize and analyze data 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

  • Parameter: data
    • Type: DataFrame.
    • Description: Source data DataFrame.
  • Parameter: values
    • Type: Column name or list of column names.
    • Description: The column to aggregate. If not specified, all numeric columns will be aggregated.
  • Parameter: index
    • Type: Column name, list of column names, or function.
    • Description: The column used as row index. Can be a single column or multiple columns (list); multiple columns create a hierarchical index.
  • Parameter: columns
    • Type: Column name or list of column names.
    • Description: The column used as column index.
  • Parameter: aggfunc
    • Type: Function, string, or list of functions.
    • Description: Aggregation function. Common values include 'sum', 'mean', 'count', 'min', 'max', 'median', 'std', etc. Default is 'mean'.
  • Parameter: fill_value
    • Type: Scalar or None.
    • Description: The value used to fill missing values.
  • Parameter: margins
    • Type: Boolean.
    • Description: Whether to add row and column totals. Default isFalse。

Function description

  • Return value: Returns a DataFrame, i.e., the pivot table.
  • Effect: Reshape and summarize data according to the specified rows, columns, and aggregation function.

Examples

Let's thoroughly master the usage ofpd.pivot_table()through a series of examples from simple to complex.

Example 1: Basic usage - Create 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]
})

print("=== Sales Data ===")
print(sales)

# 2. Create pivot table - Summarize sales by product and region
pivot = pd.pivot_table(sales, values='sales', index='product', columns='region', aggfunc='sum')
print("n=== pd.pivot_table() Sales Pivot Table ===")
print(pivot)

# 3. Use fill_value to fill missing values
print("n=== Fill missing values (fill_value=0) ===")
pivot_fill = pd.pivot_table(sales, values='sales', index='product', columns='region', aggfunc='sum', fill_value=0)
print(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 explanation:

  • The pivot table summarizes sales by product (row) and region (column).
  • fill_value=0Displays missing combinations as 0 for easier reading.

Example 2: Using different aggregation functions

aggfuncThe 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]
})

print("=== Sales Data ===")
print(sales)

# 2. Summarize (sum)
print("n=== Sum of Sales ===")
sum_result = pd.pivot_table(sales, values='sales', index='product', columns='region', aggfunc='sum')
print(sum_result)

# 3. Calculate average sales
print("n=== Average Sales ===")
mean_result = pd.pivot_table(sales, values='sales', index='product', columns='region', aggfunc='mean')
print(mean_result.round(1))

# 4. Calculate multiple statistics at once (pass a list)
print("n=== Calculate count and mean simultaneously ===")
multi_agg = pd.pivot_table(sales, values='sales', index='product', columns='region', aggfunc=['sum', 'mean'])
print(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 explanation:

  • Using different aggregation functions can calculate different statistical indicators.
  • When passing multiple aggregation functions at the same time, the pivot table creates multi-level column indexes.

Example 3: Using margins to add totals

Use themarginsparameter to add row and column totals, making it easier 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
print("=== No totals ===")
print(pd.pivot_table(sales, values='sales', index='product', columns='region', aggfunc='sum'))

# 2. Add row and column totals
print("n=== Add totals (margins=True) ===")
result = pd.pivot_table(sales, values='sales', index='product', columns='region', aggfunc='sum', margins=True)
print(result)

# 3. Custom total name
print("n=== Custom total name ===")
result_name = pd.pivot_table(sales, values='sales', index='product', columns='region', aggfunc='sum', margins=True, margins_name='Total')
print(result_name)

# 4. Totals for multi-level row index
print("n=== Multi-level row index ===")
multi_index = pd.pivot_table(sales, values='sales', index=['product', 'region'], aggfunc='sum')
print(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 explanation:

  • margins=TrueAdds "All" rows and columns, showing grand total data.
  • margins_nameYou can customize the name of the total.

Example 4: Multi-level indexing and complex pivoting

You can use multiple columns to create a pivot table with multi-level indexing.

Example

import pandas as pd

# 1. Create data with 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]
})

print("=== Data ===")
print(data)

# 2. Use multiple columns as row index
print("n=== Multi-year multi-quarter pivot ===")
pivot = pd.pivot_table(data, values='sales', index=['year', 'quarter'], columns='product', aggfunc='sum')
print(pivot)

# 3. Multiple columns as column index
print("n=== Complex cross pivoting ===")
pivot_multi = pd.pivot_table(data, values='sales', index='year', columns=['product', 'quarter'], aggfunc='sum')
print(pivot_multi)

# 4. Use fill_value to handle missing values
print("n=== Fill missing values ===")
pivot_fill = pd.pivot_table(data, values='sales', index=['year', 'quarter'], columns='product', aggfunc='sum', fill_value=0)
print(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 explanation:

  • Multi-level indexing is achieved by specifyingindex=['year', 'quarter']as a list.
  • fill_value=0Fill missing data uniformly with 0 for easier subsequent calculations.

Tip: pd.pivot_table()provides powerful data pivoting functionality. If you only need to count the frequency of two categorical variables,pd.crosstab()may be more concise.pd.crosstab()Returns frequency counts, whilepivot_table()can calculate multiple statistics such as average, sum, standard deviation, etc.pd.crosstab()can be regarded aspivot_table()Inaggfunc='count'a special case when

Pandas 常用函数Pandas common functions

Other extensions