PivotTable[tab,f,rowcol,colcol]
构造一个表格,在键为 {row,col} 位置处的值通过将函数 f 应用于与 tab 的行相对应的子表格给出,其中 rowcol 的值为 row,colcol 的值为 col.
PivotTable[tab,{key1f1,…},rowcols,colcols]
使用由 keyi 表示的多个聚合函数 fi.
PivotTable
PivotTable[tab,f,rowcol,colcol]
构造一个表格,在键为 {row,col} 位置处的值通过将函数 f 应用于与 tab 的行相对应的子表格给出,其中 rowcol 的值为 row,colcol 的值为 col.
PivotTable[tab,{key1f1,…},rowcols,colcols]
使用由 keyi 表示的多个聚合函数 fi.
更多信息和选项
- PivotTable 在统计学中也称为列联表.
- PivotTable 提供了一种汇总和重组数据的方法,通过对选定列的值指定的子表格应用聚合函数来实现.
- PivotTable[tab,f,rowcols,colcols] 生成一个 Tabular 对象,其中 rowcols 作为键列.
- 如果 colcol 规范是多个键的列表,则生成的表格将具有 ExtendedKey 的列键.
- 对于由 Flatten[{rowcols,colcols}] 确定的每个组,都会有一个子表 stab,它对应于 tab 的行子集. 聚合函数 fi 应用于由 FromTabular[stab,"Columns"] 提供的列关联.
- PivotTable[tab,{key1f1,…},rowcols,colcols] 实际上是PivotToColumns[AggregateRows[tab, {key1f1,…}, Flatten[{rowcols,colcols}]],colcols{key1,…}].
- 可以给出以下选项:
-
IncludeGroupAggregates False 是否显示边际值,给出各个分组子集的聚合结果 »
范例
打开所有单元 关闭所有单元基本范例 (2)
titanic = ResourceData["Sample Tabular Data: Titanic"]PivotTable[titanic, Mean[#age]&, "class", "sex"]PivotTable[titanic, Mean[#age]&, "class", "sex", IncludeGroupAggregates -> True]sales = ResourceData["Sample Tabular Data: Sales Data"]PivotTable[sales, Length[#Product]&, {"Region", "Sector"}, "Product"]范围 (3)
penguins = ResourceData["Sample Tabular Data: Palmer Penguins"]PivotTable[penguins, Length[#species]&, {"year", "island"}, {"species", "sex"}]titanic = ResourceData["Sample Tabular Data: Titanic"];PivotTable[titanic, {"lived" -> (Count[#survived, True]&), "died" -> (Count[#survived, False]&)}, "sex", "class"]PivotTable[titanic, {"lived" -> (Count[#survived, True]&), "died" -> (Count[#survived, False]&)}, "sex", "class", IncludeGroupAggregates -> True]titanic = ResourceData["Sample Tabular Data: Titanic"]PivotTable[titanic, PercentForm[Mean[Boole[#survived]]]&, "class", "sex"]PivotTable[titanic, PercentForm[Mean[Boole[#survived]]]&, "class", "sex", IncludeGroupAggregates -> True]选项 (1)
IncludeGroupAggregates (1)
penguins = ResourceData["Sample Tabular Data: Palmer Penguins"]PivotTable[penguins, Length[#species]&, "species", "island"]使用 IncludeGroupAggregates 获取跨行和跨列的整个类别的函数值:
PivotTable[penguins, Length[#species]&, "species", "island", IncludeGroupAggregates -> True]应用 (3)
移植患者 (1)
该数据包含有关肾移植患者在给定年龄进行移植后距死亡的时间(以天计)或在研究时间的信息:
transplants = Tabular[ResourceData["Sample Data: Kidney Transplant"]]PivotTable[transplants, UnitConvert[Mean[#Time], "Years"]&, "Race", "Gender"]PivotTable[transplants, UnitConvert[Mean[#Time], "Years"]&, "Race", "Gender", IncludeGroupAggregates -> True]显示按存活状态、性别和种族划分的平均存活时间(以年为单位):
PivotTable[transplants, UnitConvert[Mean[#Time], "Years"]&, "Delta", {"Gender", "Race"}, IncludeGroupAggregates -> True]燃油效率 (1)
data = ResourceData["Sample Tabular Data: Fuel Economy"]pt = PivotTable[ResourceData["Sample Tabular Data: Fuel Economy"], { "city_mean" -> Function[Mean[#"city"]], "hwy_mean" -> Function[Mean[#"hwy"]]}, "model", "year"]ColumnKeys[pt]diff = ConstructColumns[pt, {"model", "city_change" -> Function[#[ExtendedKey["city_mean", 2008]] - #[ExtendedKey["city_mean", 1999]]], "hwy_change" -> Function[#[ExtendedKey["hwy_mean", 2008]] - #[ExtendedKey["hwy_mean", 1999]]]}]diff = Select[diff, Count[#, x_ /; x < 0] == 2&]ListPlot[MapThread[Callout[#1, #2]&, {FromTabular[diff[All, {"city_change", "hwy_change"}], "Matrix"], Normal[diff[All, "model"]]}], PlotRange -> {{-6, 2}, {-6.5, .5}}]新生儿母亲 (1)
data = Tabular[{...}]创建一个透视表,统计吸烟的新生儿母亲数量,根据是否饮酒进行分类,并按 "motherage" 分组:
pt = PivotTable[data, ExtendedKey["smoked", "alcohol"] -> (Count[#smoke, True]&), "motherage", "alcohol"]BarChart[{pt -> ExtendedKey["smoked", "alcohol", True], pt -> ExtendedKey["smoked", "alcohol", False]}, ...]属性和关系 (2)
使用给定的聚合函数列表执行 PivotTable 操作:
titanic = ResourceData["Sample Tabular Data: Titanic"];aggrs = {"lived" -> (Count[#survived, True]&), "died" -> (Count[#survived, False]&)};PivotTable[titanic, aggrs, "sex", "class"]这相当于使用具有相同聚合的 AggregateRows,然后使用 PivotToColumns 重构结果:
AggregateRows[titanic, aggrs, {"sex", "class"}]Tabular[PivotToColumns[%, "class" -> {"lived", "died"}], <|"KeyColumns" -> {"sex"}|>]subs = Reverse@Most@Subsets[{"sex", "class"}]Map[AggregateRows[titanic, aggrs, #]&, subs]PivotTable[titanic, aggrs, "sex", "class", IncludeGroupAggregates -> True]titanic = ResourceData["Sample Tabular Data: Titanic"];pt = PivotTable[titanic, PercentForm[Mean[Boole[#survived]]]&, "class", "sex", IncludeGroupAggregates -> True]Keys[pt]使用 RowKey 来访问分组聚合值:
pt[RowKey["All"]]//Normal文本
Wolfram Research (2025),PivotTable,Wolfram 语言函数,https://reference.wolfram.com/language/ref/PivotTable.html.
CMS
Wolfram 语言. 2025. "PivotTable." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/PivotTable.html.
APA
Wolfram 语言. (2025). PivotTable. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/PivotTable.html 年
BibTeX
@misc{reference.wolfram_2026_pivottable, author="Wolfram Research", title="{PivotTable}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/PivotTable.html}", note=[Accessed: 03-December-2026]}
BibLaTeX
@online{reference.wolfram_2026_pivottable, organization={Wolfram Research}, title={PivotTable}, year={2025}, url={https://reference.wolfram.com/language/ref/PivotTable.html}, note=[Accessed: 03-December-2026]}