CaseList Class (Python)
The CaseList
class provides
access to the cases in a dataset, allowing you to read existing cases,
modify case values, and add new cases. You get an instance of the CaseList
class from the cases
property of the Dataset
class, as in:
datasetObj = spss.Dataset('data1')
caseListObj = datasetObj.cases
The number of cases in a CaseList
instance, which is also the number of cases
in the associated dataset, is available using the len
function, as in:
len(caseListObj)
Note:
An instance of the CaseList
class
can only be created within a data step, and cannot be used outside
of the data step in which it was created. Data steps are initiated
with the spss.StartDataStep
function.
Looping through
the cases in an instance of CaseList. You can loop through
the cases in an instance of the CaseList
class. For example:
for row in datasetObj.cases:
print row
- On each iteration of the loop, row is a case from the associated dataset.
Note:
The CaseList
class does not provide
any special handling for datasets with split groups--it simply returns
all cases in the dataset. If you need to differentiate the data in
separate split groups, consider using the Cursor
class to read your data, or you may want to use
the spss.GetSplitVariableNames
function to manually process the split groups.
Accessing specific cases and case values. You can access a specific case or a range of cases, and you can specify a variable or a range of variables within those cases. The result is a list, even if accessing the value of a single variable within a single case.
- System-missing values are returned as the Python data type None.
- Values of variables with
TIME
andDTIME
formats are returned as integers representing the number of seconds in the time interval. - By default, values of variables with date or datetime
formats are returned as integers representing the number of seconds
from October 14, 1582. You can specify to convert values of those
variables to Python
datetime.datetime
objects with the cvtDates argument to theDataset
class. See the topic spss.Dataset Class (Python) for more information.
Example: Accessing a Single Case
Case values are accessed by specifying the case number, starting with 0, as in:
oneCase = datasetObj.cases[0]
Case values are returned as a list where each element of the list is the value of the associated variable.
Example: Accessing a Single Value Within a Case
You can access the value for a single variable within a case by specifying the case number and the index of the variable (index values represent position in the active dataset, starting with 0 for the first variable in file order). The following gets the value of the variable with index 1 for case number 0.
oneValue = datasetObj.cases[0,1]
Note that oneValue is a list with a single element.
Example: Accessing a Range of Values
You can use the Python slice notation to specify ranges of cases and ranges of variables within a case. Values for multiple cases are returned as a list of elements, each of which is a list of values for a single case.
# Get the values for cases 0,1, and 2
data = datasetObj.cases[0:3]
# Get the values for variables with index values 0,1, and 2
# for case number 0
data = datasetObj.cases[0,0:3]
# Get the value for the variable with index 1 for case numbers 0,1, and 2
data = datasetObj.cases[0:3,1]
# Get the values for the variables with index values 1,2 and 3
# for case numbers 4,5, and 6
data = datasetObj.cases[4:7,1:4]
Example: Negative Index Values
Case indexing supports the use of negative indices, both for the case number and the variable index. The following gets the value of the second to last variable (in file order) for the last case.
value = datasetObj.cases[-1,-2]
Modifying case values. You can modify the values for a specific case or a range of cases, and you can set the value of a particular variable or a range of variables within those cases.
- 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
ordatetime.datetime
objects, which are then converted to the appropriate IBM® SPSS® Statistics value. Values of variables withTIME
andDTIME
formats should be specified as the number of seconds in the time interval.
Example: Setting Values for a Single Case
Values for a single case are provided as a list or tuple of values. The first element corresponds to the first variable in file order, the second element corresponds to the second variable in file order, and so on. Case numbers start from 0.
datasetObj.cases[1] = [35,150,100,2110,19,2006,3,4]
Example: Setting a Single Value Within a Case
You can set the value for a single variable within a case by specifying the case number and the index of the variable (index values represent position in the active dataset, starting with 0 for the first variable in file order). The following sets the value of the variable with index 0 for case number 12 (case numbers start from 0).
datasetObj.cases[12,0] = 14
Example: Setting Ranges of Values
You can use the Python slice notation to specify ranges of cases and ranges of variables within a case. Values for multiple cases are specified as a list or tuple of elements, each of which is a list or tuple of values for a single case.
# Set the values for cases 0,1, and 2
datasetObj.cases[0:3] = ([172,'m',27,34500],[67,'f',32,32500],
[121,'f',37,23000])
# Set the values for variables with index values 5,6, and 7 for
# case number 34
datasetObj.cases[34,5:8] = [70,1,4]
# Set the value for the variable with index 5 for case numbers 0,1, and 2
datasetObj.cases[0:3,5] = [70,72,71]
# Set the values for the variables with index values 5 and 6 for
# case numbers 4,5, and 6
datasetObj.cases[4:7,5:7] = ([70,1],[71,2],[72,2])
Example: Negative Index Values
Case indexing supports the use of negative indices, both for the case number and the variable index. The following specifies the value of the second to last variable (in file order) for the last case.
datasetObj.cases[-1,-2] = 8
Deleting cases. You can delete a specified case from the CaseList
object, which results in deleting that case
from the associated dataset. For example:
del datasetObj.cases[0]