I see lots of examples of updating a specific column down ALL rows of a table, for example:
with arcpy.da.UpdateCursor(Input_Layer, ['Column_1','Column_2']) as cursor:
for row in cursor:
row.setValue('Column_1', value1)
row.setValue('Column_2', value2)
...but how do you update a specific field in JUST ONE row (specifically the bottom, currently blank row)?
Selecting specific row in attribute table using ArcPy (without iterating through whole table)? explains how to point to a specific row that already exists, but in my case the row/column I want to update is currently blank and has no other columns I can use to specify it.
My code will begin a loop, gather 3 numerical values (based on calculations from other columns in the table), and then store those 3 values in 3 (initially) blank "tally columns", but again, only in the first row .
The next time through the loop the three "tally columns" would be updated with the newly calculated values, but now only in row 2.
And then row 3, etc...
The following image shows my original table (green columns) plus the summary statistics that I wish to add (orange columns) by iterating through multiple uses of SelectLayerByAttribute_management
and then UpdateCursor
.
The image tries to capture a point during my repeated looping where SelectLayerByAttribute_management
is fed the following values...
value1 = "01"
value2 = "030"
value3 = "003"
...for the following query.
query = '"{0}" = {1} AND "{2}" = {3} AND "{4}" = {5}'.format(column1, value1, column2, value2, column3, value3)
...and then updates the 3 "tally columns" in just the highlighted row you see in the orange section (which at that time is the newest row at the bottom).
Here's the Key: Each time through the code will update the "tally columns" in only the bottom row (which is initially blank) .
How can I refer to that bottom, initially blank row? Using an index? For example, if I employed a counter variable, x, starting at 0 and then increased it by one (x+=1) each time through the loop, something like this would work?
with arcpy.da.UpdateCursor(Input_Layer, ['locator','area','sum']) as cursor:
row[x].setValue('locator', value1)
row[x].setValue('area', value2)
row[x].setValue('sum', value2)
Or perhaps a Where Clause will be useful? But in that case I would be interested in "the next row that is currently blank" and I don't know how to code that statement.
Answer
In your picture your orange "table" looks like it could be easily derived from your green "table" using the Summary Statistics tool with square_km SUM as your statistics field and three case fields (column1, column2 and column3).
No comments:
Post a Comment