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.

Details and Options

  • 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

Examples

open allclose all

Basic Examples  (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:

Applications  (1)

The data contains information on kidney transplant patientstime in weeks 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:

Properties & Relations  (1)

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:

Wolfram Research (2025), PivotTable, Wolfram Language function, https://reference.wolfram.com/language/ref/PivotTable.html.

Text

Wolfram Research (2025), PivotTable, Wolfram Language function, https://reference.wolfram.com/language/ref/PivotTable.html.

CMS

Wolfram Language. 2025. "PivotTable." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/PivotTable.html.

APA

Wolfram Language. (2025). PivotTable. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PivotTable.html

BibTeX

@misc{reference.wolfram_2024_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_2024_pivottable, organization={Wolfram Research}, title={PivotTable}, year={2025}, url={https://reference.wolfram.com/language/ref/PivotTable.html}, note=[Accessed: 15-January-2025 ]}