-
Notifications
You must be signed in to change notification settings - Fork 14
Python API Access Tabular Data
As usual in tables you can access either the data of a column or of a row. To access the data in a certain column you can simply use column() and then either pass it the name of the column (e.g. "OCTOBER") or the column index (e.g. 10). The method will then return a tuple with all the values of this column.
To get access to the data of a specific row you can use the record() method and pass it hte number of the row you want to access.
fc = FeatureCoverage("rainfall.shp")
table = fc.attributeTable()
octVal = table.column("OCTOBER")
# gives exactly the same result as:
octVal = table.column(10)
# octVal includes: (48, 46, 86, 89, 44, 40, 44, 85, 89, 0, 0, 0, 0)
santaRosa = table.record(2)
# santaRosa includes: ('Laguna_Santa_Rosa', 175, 165, 160, 78, 54, 35, 16, 4, 20, 86, 173, 181, 340, 2)In some cases you certainly don't need the whole colum or row but are only looking for some specific data. In IlwisObjects there are different ways to get this data from the table.
If you're looking for data that matches to specific conditions like "MARCH < 100 AND MARCH != 87", your best choice would be the select() method. You can simply pass your query as a string and it will return the rows that have a value smaller 100 and unequal to 87 in the MARCH column.
# We just assume we have the rainfall table loaded into our 'table' variable
result = table.select("MARCH < 100 AND MARCH != 87")
# the content of result will be (4, 5, 6), which indicates the row/record indexesAnother way to get specific data is by just exactly selecting the cell you want. This is done by using the cell() method. As first argument it takes the column name or index and as the second the row you want the data out of.
result = table.cell("march", 4)Of course you can also combine both methods of accessing data to make them even more useful. A very pythonic way to do this is shown in the next example:
colInd = table.columnIndex("MARCH")
result = [table.cell(colInd, rec) for rec in table.select("MARCH < 100 AND MARCH != 87")]
# result = [81, 76, 79]This way you get the record indexes from the query and can directly use it in the cell selection to get the data from the according cells.