SimplePivotTable Method (Python)

.SimplePivotTable(rowdim,rowlabels,coldim,collabels,cells). Creates a pivot table with one row dimension and one column dimension.

  • rowdim. An optional label for the row dimension, given as a string. If empty, the row dimension label is hidden. If specified, it must be distinct from the value, if any, of the coldim argument.
  • rowlabels. An optional list of items to label the rows. Each item must be unique and can be a character string, a numeric value, or a CellText object (one of CellText.Number, CellText.String, CellText.VarName, or CellText.VarValue). If provided, the length of this list determines the number of rows in the table. If omitted, the number of rows is equal to the number of elements in the argument cells.
  • coldim. An optional label for the column dimension, given as a string. If empty, the column dimension label is hidden. If specified, it must be distinct from the value, if any, of the rowdim argument.
  • collabels. An optional list of items to label the columns. Each item must be unique and can be a character string, a numeric value, or a CellText object (one of CellText.Number, CellText.String, CellText.VarName, or CellText.VarValue). If provided, the length of this list determines the number of columns in the table. If omitted, the number of columns is equal to the length of the first element of cells. If cells is one-dimensional, this implies a table with one column and as many rows as there are elements in cells. See the examples below for the case where cells is two-dimensional and collabels is omitted.
  • cells. This argument specifies the values for the cells of the pivot table. It consists of a one- or two-dimensional sequence of items that can be indexed as cells[i] or cells[i][j]. For example, [1,2,3,4] is a one-dimensional sequence, and [[1,2],[3,4]] is a two-dimensional sequence. Each element in cells can be a character string, a numeric value, a CellText object (one of CellText.Number, CellText.String, CellText.VarName, or CellText.VarValue), a Python time.struct_time object, or a Python datetime.datetime object. Examples showing how the rows and columns of the pivot table are populated from cells are provided below.
  • The number of elements in cells must equal the product of the number of rows and the number of columns.
  • Elements in the pivot table are populated in row-wise fashion from the elements of cells. For example, if you specify a table with two rows and two columns and provide cells=[1,2,3,4], the first row will consist of the first two elements and the second row will consist of the last two elements.
  • Numeric values specified in cells, rowlabels, or collabels will be converted to CellText.Number objects with a format given by the default. The default format can be set with the SetDefaultFormatSpec method and retrieved with the GetDefaultFormatSpec method. Instances of the BasePivotTable class have an implicit default format of GeneralStat.
  • String values specified in cells, rowlabels, or collabels will be converted to CellText.String objects.
  • When specifying cell values with Python time.struct_time or datetime.datetime objects, the value will be displayed in seconds--specifically, the number of seconds from October 14, 1582. You can change the format of a cell to a datetime format using the SetNumericFormatAt method (Python) Python Scripting method.

Example: Creating a Table with One Column

import spss
spss.StartProcedure("mycompany.com.demoProc")

table = spss.BasePivotTable("Table Title",
                            "OMS table subtype")
table.SimplePivotTable(rowdim="row dimension",
                       rowlabels=["row 1","row 2","row 3","row 4"],
                       collabels=["column 1"],
                       cells = [1,2,3,4])
spss.EndProcedure()

Result

Figure 1. Pivot table with a single column
Pivot table with a single column

Example: Using a Two-Dimensional Sequence for Cells

import spss
spss.StartProcedure("mycompany.com.demoProc")

table = spss.BasePivotTable("Table Title",
                            "OMS table subtype")
table.SimplePivotTable(rowdim="row dimension",
                       coldim="column dimension",
                       rowlabels=["row 1","row 2","row 3","row 4"],
                       collabels=["column 1","column 2"],
                       cells = [[1,2],[3,4],[5,6],[7,8]])
spss.EndProcedure()

Result

Figure 2. Table populated from two-dimensional sequence
Table populated from two-dimensional sequence

Example: Using a Two-Dimensional Sequence for Cells and Omitting Column Labels

import spss
spss.StartProcedure("mycompany.com.demoProc")

table = spss.BasePivotTable("Table Title",
                            "OMS table subtype")
table.SimplePivotTable(rowdim="row dimension",
                       coldim="column dimension",
                       rowlabels=["row 1","row 2","row 3","row 4"],
                       cells = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
spss.EndProcedure()

Result

Figure 3. Table populated from two-dimensional sequence without specifying column labels
Table populated from two-dimensional sequence without specifying column labels