All code snippets on this page will reference the cities dataset we created above, so place that code at the beginning of every code snippet.
Accessing Data in a Dataset
To access the data inside of a dataset, each dataset has a few functions that can be called on to access different parts of the dataset. These are listed in the table below.
|data.getColumnCount()||Returns the number of columns in the dataset.||4|
|data.getColumnIndex(colName)||Returns the index of the column with the name colName.||2|
|data.getColumnName(colIndex)||Returns the name of the column at the index colIndex.||Population|
|data.getColumnNames()||Returns a list with the names of all the columns.||[City, Population, Timezone, GMTOffset]|
|data.getColumnType(colIndex)||Returns the type of the column at the index colIndex.|
|data.getColumnTypes()||Returns a list with the types of all the columns.||[class java.lang.String, class java.lang.Integer, class java.lang.String, class java.lang.Integer]|
|data.getRowCount()||Returns the number of rows in the dataset.||5|
|data.getValueAt(rowIndex, colIndex)||Returns the value at the specified row and column indexes.||PST|
|data.getValueAt(rowIndex, colName)||Returns the value at the specified row index and column name.||2853114|
Looping Through a Dataset
Oftentimes you need to loop through the items in a dataset similar to how you would loop through a list of items. You can use the functions above to do this.
Accessing Data in a PyDataset
PyDatasets can be accessed in the same ways that Datasets can. This means that all of the above functions ( getColumnCount(), getValueAt(), etc ) can be used with PyDatasets too.
PyDatasets are special in that they can be handled similarly to other Python sequences. Any dataset object can be converted to a PyDataset using the function system.dataset.toPyDataSet. All of the functions listed above can be used on a PyDataset, but the data can also be accessed much easier, similar to how you would a list.
Looping Through a PyDataset
Looping through a PyDataset is also a bit easier to do, working similar to other sequences. The first for loop will pull out each row, which acts like a list and can be used in a second for loop to extract the values.
Additionally, a single column of data can be extracted by looping through the PyDataset.
A PyRow is a row in a PyDataset. It works similarly to a Python list.In 8.0.14, we added the following methods to PyRows. The examples and outputs are based on the results in the table below. In addition, "print" commands are used, but should be replaced by appropriate logging methods (such as system.util.getLogger) depending on the scope of the script.
Returns the index of first occurrence of the element. Returns a ValueError if the element isn't present in the list.
|count()||Calculates total occurrence of given element in the row.|
8.0.14 also introduces the concept of repeating elements in a row:
Altering a Dataset
Technically, you cannot alter a dataset. Datasets are immutable, meaning they cannot change. You can, however, create new datasets. To change a dataset, you really create a new one and then replace the old one with the new one. There are system functions that are available that can alter or manipulate datasets in other ways. Any of the functions in the system.dataset section can be used on datasets, the most common ones have been listed below:
The important thing to realize about all of these datasets is that, again, they do not actually alter the input dataset. They return a new dataset. You need to actually use that returned dataset to do anything useful.
For example, the following code is an example of the setValue function, and would change the population value for Los Angeles.