insert Method (Python)

.insert(case, caseNumber). Inserts a new case into the associated dataset and inserts an element representing the case into the corresponding CaseList instance. The argument case is a tuple or list specifying the case values. The first element in the tuple or list is the value for the first variable in file order, the second is the value of the second variable in file order and so on. The optional argument caseNumber specifies the location at which the case is inserted (case numbers start from 0) and can take on the values 0,1,...,n where n is the number of cases in the dataset. If caseNumber is omitted or equal to n, the case is appended.

  • The elements of case can be numeric or string values and must match the variable type of the associated variable. Values of None are converted to system-missing for numeric variables and blanks for string variables.
  • Values of numeric variables with a date or datetime format should be specified as Python time.struct_time or datetime.datetime objects, which are then converted to the appropriate IBM® SPSS® Statistics value. Values of variables with TIME and DTIME formats should be specified as the number of seconds in the time interval.

Example

DATA LIST FREE/numvar (F2) strvar (A1).
BEGIN DATA.
1 a
3 c
END DATA.
BEGIN PROGRAM.
import spss
spss.StartDataStep()
datasetObj = spss.Dataset()
# Insert a single case into the active dataset at case number 1
datasetObj.cases.insert([2,'b'],1)
spss.EndDataStep()
END PROGRAM.