valueLabels Property (Python)
The valueLabels
property of
a Variable
object gets or sets
value labels. It can also be used to clear any value labels. The valueLabels
property behaves like a Python
dictionary in terms of getting, setting, and deleting values. A Python
dictionary consists of a set of keys, each of which has an associated
value that can be accessed simply by specifying the key. In the case
of value labels, each key is a value and the associated value is the
label.
- When setting value labels for string variables, values must be specified as quoted strings.
Retrieving Value
Labels. You retrieve value labels for a specified variable
from the valueLabels
property
of the associated Variable
object.
You retrieve the label for a particular value by specifying the value,
as in the following, which retrieves the label for the value 1:
varObj = datasetObj.varlist['origin']
valLab = varObj.valueLabels[1]
You can iterate through the set of value labels
for a variable using the data
property, as in:
varObj = datasetObj.varlist['origin']
for val, valLab in varObj.valueLabels.data.iteritems():
print val, valLab
Adding and Modifying Value Labels. You can add new value labels and modify existing ones. For example:
varObj = datasetObj.varlist['origin']
varObj.valueLabels[4] = 'Korean'
- If a label for the value 4 exists, its value is updated to 'Korean'. If a label for the value 4 doesn't exist, it is added to any existing value labels for the variable origin.
Resetting Value Labels. You can reset the value labels to a new specified set. For example:
varObj = datasetObj.varlist['origin']
varObj.valueLabels = {1:'American',2:'Japanese',3:'European',
4:'Korean',5:'Chinese'}
- You reset the value labels by setting the valueLabels property to a new Python dictionary. Any existing value labels for the variable are cleared and replaced with the specified set.
Deleting Value Labels. You can delete a particular value label or the entire set of value labels for a specified variable. For example:
varObj = datasetObj.varlist['origin']
#Delete the value label for the value 1
del varObj.valueLabels[1]
#Delete all value labels
del varObj.valueLabels