-
Notifications
You must be signed in to change notification settings - Fork 13
Python API Tables
For the basic operations like loading, accessing and altering of tables, please have a look at the according How to... pages:
Once you have initialized a table you can use it's data for further processing. The following example shows a way to extract the data of the rainfall table and put it into a dictionary.
places = {}
for x in range(0, table.recordCount()-1):
values = table.record(x)
name = str(values[0])
values = values[1:13]
numrange = NumericRange(min(values), max(values))
places[name] = numrange
for place in places:
print(place, places[place])
To understand the example, you first have to know what the table consists of. The first column of the table consists of the names of different places from where the rainfall was measured. The following twelve columns are values that represent the average rainfall for each place for each month. In the end there are three additional columns that are not interesting for our data analysis.
First we initalize an empty dictionary, called places
. Next we iterate over the table.record
values from 0, to table.recordCount()-1 (as we start at 0 we have to substract one from the total size).
Next we get the table record at position x, by simply calling table.record(x)
. We get a tuple from this method, which we store in a variable called values
. The tuple now consists of 16 elements and we know that the first one is the name of the place where the rainfall was measured, so we can extract this one and turn it into a string, by simply using str(values[0])
.
Next we slice the tuple, because we only want the twelve values that follow the name, as they represent the values of the twelve months, so we call values[1:13]
. Now values
only consists of twelve elements.
In the next step we create a NumericRange
and pass it the minimum and the maximum of the tuple. For more information on NumericRanges have a look at the Python API Tutorial.
In the next step we fill the places
dictionary, with the name of the place and the according NumericRange.
After we have done this for all rows in the table we can't print them. The NumericRange string represantation gives you it's minimum and maximum. So the printed output will look like this: Laguna_Santa_Rosa 4.0 181.0
.