pandas.DataFrame.pivot_table#

DataFrame.pivot_table(values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All', observed=_NoDefault.no_default, sort=True)[源代码]#

创建电子表格风格的透视表作为 DataFrame。

透视表中的级别将存储在结果 DataFrame 的索引和列上的 MultiIndex 对象(分层索引)中。

Parameters:
values类似列表或标量,可选

要聚合的列或列。

index列、Grouper、数组或上述列表

用于对透视表索引进行分组的键。如果传递列表,它可以包含任何其他类型(列表除外)。如果传递数组,它必须与数据长度相同,并将以与列值相同的方式使用。

columns列、Grouper、数组或上述列表

用于对透视表列进行分组的键。如果传递列表,它可以包含任何其他类型(列表除外)。如果传递数组,它必须与数据长度相同,并将以与列值相同的方式使用。

aggfunc函数、函数列表、字典,默认值为 “mean”

如果传递函数列表,则生成的透视表将具有分层列,其顶层是函数名称(从函数对象本身推断)。如果传递字典,键是需要聚合的列,值是函数或函数列表。如果设置了 margin=True,则 aggfunc 将用于计算部分聚合。

fill_value标量,默认为 None

用于替换缺失值的值(在生成的透视表中,聚合后)。

marginsbool,默认 False

如果 margins=True,将添加特殊的 All 列和行,其中包含行和列上各类别之间的部分组聚合。

dropnabool, default True

不要包含条目全部为 NaN 的列。如果为 True,则在计算边距之前将省略在任何列中具有 NaN 值的行。

margins_name字符串,默认值为 ‘All’

当 margins 为 True 时,包含总计的行/列的名称。

observedbool,默认 False

这仅在任何分组器是 Categorical 时才适用。如果为 True:仅为分类分组器显示观察到的值。如果为 False:为分类分组器显示所有值。

自 2.2.0 版本弃用: False 的默认值已弃用,将在 pandas 的未来版本中更改为 True

sortbool, default True

指定结果是否应排序。

在 1.3.0 版本加入.

Returns:
DataFrame

类似 Excel 的透视表。

参见

DataFrame.pivot

不带聚合的透视,可以处理非数值数据。

DataFrame.melt

将 DataFrame 从宽格式重塑为长格式,并可选择保留标识符。

wide_to_long

将宽面板数据转换为长格式。灵活性不如 melt,但更易于使用。

Notes

参考 the user guide 获取更多示例。

Examples

>>> df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
...                          "bar", "bar", "bar", "bar"],
...                    "B": ["one", "one", "one", "two", "two",
...                          "one", "one", "two", "two"],
...                    "C": ["small", "large", "large", "small",
...                          "small", "large", "small", "small",
...                          "large"],
...                    "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
...                    "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
>>> df
     A    B      C  D  E
0  foo  one  small  1  2
1  foo  one  large  2  4
2  foo  one  large  2  5
3  foo  two  small  3  5
4  foo  two  small  3  6
5  bar  one  large  4  6
6  bar  one  small  5  8
7  bar  two  small  6  9
8  bar  two  large  7  9

第一个示例通过求和来聚合值。

>>> table = pd.pivot_table(df, values='D', index=['A', 'B'],
...                        columns=['C'], aggfunc="sum")
>>> table
C        large  small
A   B
bar one    4.0    5.0
    two    7.0    6.0
foo one    4.0    1.0
    two    NaN    6.0

我们也可以使用 fill_value 参数填充缺失值。

>>> table = pd.pivot_table(df, values='D', index=['A', 'B'],
...                        columns=['C'], aggfunc="sum", fill_value=0)
>>> table
C        large  small
A   B
bar one      4      5
    two      7      6
foo one      4      1
    two      0      6

下一个示例通过跨多个列取平均值来进行聚合。

>>> table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
...                        aggfunc={'D': "mean", 'E': "mean"})
>>> table
                D         E
A   C
bar large  5.500000  7.500000
    small  5.500000  8.500000
foo large  2.000000  4.500000
    small  2.333333  4.333333

我们也可以为任何给定的值列计算多种类型的聚合。

>>> table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
...                        aggfunc={'D': "mean",
...                                 'E': ["min", "max", "mean"]})
>>> table
                  D   E
               mean max      mean  min
A   C
bar large  5.500000   9  7.500000    6
    small  5.500000   9  8.500000    8
foo large  2.000000   5  4.500000    4
    small  2.333333   6  4.333333    2