Function Repository Resource:

PivotTable

Source Notebook

A spreadsheet-like operation to collate data by values common to specified columns, summarizing corresponding values in a specified column

Contributed by: Kelvin Mischo

ResourceFunction["PivotTable"][{{label1,label2,},{i1,i2,},{i3,i4,}},binlabel,valuecolumn,func]

collates values of valuecolumn common to binlabel and applies func to them.

ResourceFunction["PivotTable"][{{label1,label2,},{i1,i2,},{i3,i4,}}, binlabel1,binlabel2,valuelabel,func]

collates values common to the two bin labels.

Details and Options

The output contains as many rows as unique elements in binlabel or binlabel1.
The output contains two columns if one binlabel is specified, and three columns if a binlabel1 and binlabel2 are specified.
Common values for func are Total or Length, but any other function can be used.
The first argument can be a nested list or a variable storing an imported dataset as a nested list.

Examples

Basic Examples (2) 

Get the Total of the "Second" column with a common name in the "First" column:

In[1]:=
ResourceFunction[
 "PivotTable"][{{"First", "Second"}, {"One", 2}, {"One", 3}}, "First", "Second", Total]
Out[1]=

Example dataset with labels in the first rows:

In[2]:=
dat = {{"City", "State", "Item 1", "Item 2"}, {"Champaign", "IL", 2, 13.50}, {"Champaign", "IL", 2, 6.25}, {"Champaign", "MI", 10, 12.10}, {"Chicago", "IL", 15, 16.76}, {"Springfield", "IL", 18, 19.01}};
In[3]:=
TextGrid[dat]
Out[3]=

List all unique values in the column "City" and sum the corresponding numeric values in "Item 1" using Total:

In[4]:=
ResourceFunction["PivotTable"][dat, "City", "Item 1", Total]
Out[4]=

Perform the same calculation, but format in a grid with TextGrid:

In[5]:=
TextGrid[
 ResourceFunction["PivotTable"][dat, "City", "Item 1", Total]
 ]
Out[5]=

List all unique values in the column "City" and sum the corresponding numeric values in "Item 2" using Total; use TextGrid in postfix form:

In[6]:=
ResourceFunction["PivotTable"][dat, "City", "Item 2", Total] // TextGrid
Out[6]=

List all unique values common to both the "City" and "State" columns and sum the corresponding numeric values in "Item 1" using Total:

In[7]:=
ResourceFunction["PivotTable"][dat, "City", "State", "Item 1", Total] // TextGrid
Out[7]=

List all unique values common to both the "City" and "State" columns and sum the corresponding numeric values in "Item 2" using Total:

In[8]:=
ResourceFunction["PivotTable"][dat, "City", "State", "Item 2", Total] // TextGrid
Out[8]=

Scope (6) 

Example dataset with labels in the first rows:

In[9]:=
dat = {{"City", "State", "Item 1", "Item 2"}, {"Champaign", "IL", 2, 13.50}, {"Champaign", "IL", 2, 6.25}, {"Champaign", "MI", 10, 12.10}, {"Chicago", "IL", 15, 16.76}, {"Springfield", "IL", 18, 19.01}};

List all unique values common to both the "City" and "State" columns and count the corresponding numeric values in "Item 2" using Length:

In[10]:=
ResourceFunction["PivotTable"][dat, "City", "Item 2", Length] // TextGrid
Out[10]=

List all unique values common to both the "City" and "State" columns and count the corresponding numeric values in "Item 1" using Length:

In[11]:=
ResourceFunction["PivotTable"][dat, "City", "State", "Item 2", Length] // TextGrid
Out[11]=

Perform the same calculation, with additional options for TextGrid to add a gray frame:

In[12]:=
TextGrid[ResourceFunction["PivotTable"][dat, "City", "State", "Item 2", Length], Frame -> All, FrameStyle -> Directive[Gray]]
Out[12]=

Perform the same calculation, with different columns and Total to sum values in "Item 1":

In[13]:=
TextGrid[ResourceFunction["PivotTable"][dat, "City", "State", "Item 1", Total], Frame -> All, FrameStyle -> Directive[Gray]]
Out[13]=

Although strings are commonly binned with pivot tables, numeric values can be used as well:

In[14]:=
TextGrid[ResourceFunction["PivotTable"][dat, "Item 1", "City", Length], Frame -> All, FrameStyle -> Directive[Gray]]
Out[14]=

Applications (2) 

Example dataset with labels in the first rows:

In[15]:=
dat = {{"City", "State", "Item 1", "Item 2"}, {"Champaign", "IL", 2, 13.50}, {"Champaign", "IL", 2, 6.25}, {"Champaign", "MI", 10, 12.10}, {"Chicago", "IL", 15, 16.76}, {"Springfield", "IL", 18, 19.01}};

Make a Table of pivot tables to compare values:

In[16]:=
Table[{
   i,
   TextGrid[
    ResourceFunction["PivotTable"][dat, "City", "State", i, Total], Frame -> All, FrameStyle -> Directive[Gray]]},
  {i, {"Item 1", "Item 2"}}
  ] // TextGrid
Out[16]=

Example dataset with labels in the first rows:

In[17]:=
dat = {{"City", "State", "Item 1", "Item 2"}, {"Champaign", "IL", 2, 13.50}, {"Champaign", "IL", 2, 6.25}, {"Champaign", "MI", 10, 12.10}, {"Chicago", "IL", 15, 16.76}, {"Springfield", "IL", 18, 19.01}};

Perform a simple pivot table and export the resulting list:

In[18]:=
ResourceFunction["PivotTable"][dat, "City", "State", "Item 1", Total]
Out[18]=
In[19]:=
Export["Pivot-Test.xls", %]
Out[19]=

Neat Examples (2) 

Example dataset with labels in the first rows:

In[20]:=
dat = {{"City", "State", "Item 1", "Item 2"}, {"Champaign", "IL", 2, 13.50}, {"Champaign", "IL", 2, 6.25}, {"Champaign", "MI", 10, 12.10}, {"Chicago", "IL", 15, 16.76}, {"Springfield", "IL", 18, 19.01}};

Make a Manipulate with varying column values for a pivot table:

In[21]:=
Manipulate[
 TextGrid[
  ResourceFunction["PivotTable"][dat, "City", "State", i, Total], Frame -> All, FrameStyle -> Directive[Gray]],
 {i, {"Item 1", "Item 2"}},
 SaveDefinitions -> True
 ]
Out[21]=

Create a pivot table for a large dataset; first create a large test dataset:

In[22]:=
values1 = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
values2 = {"One", "Two", "Three", "Four"};
planets = Prepend[
   Table[{
     RandomChoice[values1],
     RandomChoice[values2],
     RandomReal[],
     RandomReal[]
     },
    {i, 1200000}
    ],
   {"Planet", "Quarter", "Value 1", "Value 2"}
   ];
In[23]:=
Export["test-large.csv", planets]
Out[23]=

Import the test dataset with 1.2 million rows and 4 columns:

In[24]:=
largeData = Import["test-large.csv", "Data"];
In[25]:=
Dimensions[largeData]
Out[25]=

Create a pivot table based on the large dataset:

In[26]:=
ResourceFunction["PivotTable"][largeData, "Planet", "Quarter", "Value 1", Total] // TextGrid
Out[26]=

Publisher

Kelvin Mischo

Version History

  • 1.0.0 – 12 August 2019

License Information