Creating Pivot Tables with the SimplePivotTable Method (Python)

For creating a pivot table with a single row dimension and a single column dimension, the BasePivotTable class provides the SimplePivotTable method. The arguments to the method provide the dimensions, categories, and cell values. No other methods are necessary in order to create the table structure and populate the cells. If you require more functionality than the SimplePivotTable method provides, there are a variety of methods to create the table structure and populate the cells. See the topic General Approach to Creating Pivot Tables (Python) for more information.

Example

import spss
spss.StartProcedure("mycompany.com.demoProc")
table = spss.BasePivotTable("Table Title",
                            "OMS table subtype")
                             
table.SimplePivotTable(rowdim = "row dimension",
                       rowlabels = ["first row","second row"],
                       coldim = "column dimension",
                       collabels = ["first column","second column"],
                       cells = [11,12,21,22])
spss.EndProcedure()

Result

Figure 1. Simple pivot table
Simple pivot table
  • This example shows how to generate a pivot table within a spss.StartProcedure-spss.EndProcedure block. The argument to the StartProcedure function specifies a name to associate with the output. This is the name that appears in the outline pane of the Viewer associated with the output--in this case, mycompany.com.demoProc. It is also the command name associated with this output when routing output with OMS.

    Note: In order that names associated with output do not conflict with names of existing IBM® SPSS® Statistics commands (when working with OMS), it is recommended that they have the form yourcompanyname.com.procedurename. See the topic spss.StartProcedure Function (Python) for more information.

  • You create a pivot table by first creating an instance of the BasePivotTable class and storing it to a variable--in this case, the variable table.
  • The SimplePivotTable method of the BasePivotTable instance is called to create the structure of the table and populate its cells. Row and column labels and cell values can be specified as character strings or numeric values. They can also be specified as a CellText object. CellText objects allow you to specify that category labels be treated as variable names or variable values, or that cell values be displayed in one of the numeric formats used in IBM SPSS Statistics pivot tables, such as the format for a mean. When you specify a category as a variable name or variable value, pivot table display options such as display variable labels or display value labels are honored.
  • Numeric values specified for cell values, row labels, or column labels, are displayed using the default format for the pivot table. Instances of the BasePivotTable class have an implicit default format of GeneralStat. You can change the default format using the SetDefaultFormatSpec method.
  • spss.EndProcedure marks the end of output creation.