PivotTable

PivotTable[tab,f,rowcol,colcol]

constructs a table where the value at the position with keys {row,col} is given by applying the function f to the subtabulars corresponding to the rows of tab where rowcol has value row and colcol has value col.

PivotTable[tab,{key1f1,},rowcols,colcols]

uses multiple aggregation functions fi denoted by keyi.

更多信息和选项

  • PivotTable provides a way to summarize and reorganize data by applying an aggregation function to subtabulars specified by values of selected columns.
  • PivotTable[tab,f,rowcols,colcols] produces a Tabular object in which rowcols are key columns.
  • If the colcol specification is a list of several keys, the resulting table will have ExtendedKey column keys.
  • For each group determined by Flatten[{rowcols,colcols}], there is a subtabular stab corresponding to a subset of rows of tab. The aggregation functions fi are applied to the association of columns given by FromTabular[stab,"Columns"].
  • PivotTable[tab,{key1f1,},rowcols,colcols] is effectively PivotToColumns[AggregateRows[tab, {key1f1,}, Flatten[{rowcols,colcols}]],{colcols,{key1,}}].
  • The following option can be given:
  • IncludeGroupAggregatesFalsewhether to show margins giving the aggregation for subsets of the groups »

范例

打开所有单元关闭所有单元

基本范例  (2)

Make a pivot table showing the percent survival for the Titanic:

Percent survived per class and per sex:

Show margins with the survival rate for group subsets:

Take tabular data about sales:

Construct a pivot table showing number of product types per region and sector:

Scope  (2)

Take a table of data about penguins in the Palmer Archipelago:

Construct a pivot table specifying the output row keys and column keys by multiple input columns:

Show the number that lived and died for the Titanic:

Show group aggregate summaries:

Options  (1)

IncludeGroupAggregates  (1)

Take a table of data about penguins in the Palmer Archipelago:

Create pivot table to count the number of penguins per species and per island:

Use IncludeGroupAggregates to get the values of the function for the whole category across the rows and across the columns:

Applications  (2)

Transplant Patients  (1)

The data contains information on kidney transplant patientstime in days to death or on-study time since transplant at the given age:

Show the mean survival time in years split according to gender and race:

Show group aggregate summaries:

Show the mean survival time in years split according to alive status versus gender and race:

Fuel Efficiency  (1)

Take data for a selection of car models:

Find the mean city and highway milage per each model and each year:

Find the change in mean milage from 1999 to 2008:

Select the models where both city and highway mileage decreased:

Properties & Relations  (2)

Perform a PivotTable operation with a given list of aggregation functions:

This is equivalent to using AggregateRows with the same aggregations, followed by PivotToColumns to restructure the result:

The group aggregate summaries can be obtained as follows:

Compare to the group aggregation row and columns of this result:

Pivot tables have row keys:

Extract the list of row keys:

Use RowKey to access group aggregate values:

Wolfram Research (2025),PivotTable,Wolfram 语言函数,https://reference.wolfram.com/language/ref/PivotTable.html.

文本

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_2025_pivottable, author="Wolfram Research", title="{PivotTable}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/PivotTable.html}", note=[Accessed: 15-January-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_pivottable, organization={Wolfram Research}, title={PivotTable}, year={2025}, url={https://reference.wolfram.com/language/ref/PivotTable.html}, note=[Accessed: 15-January-2025 ]}