I'd like to be able to use an update cursor to go through rows in a table, holding onto the previous rows value, and comparing it to the current row value, to populate a new field with this value. Does anyone know if it is possible to carry values forward to the next iteration of a loop while using an update cursor and what would this look like in code?
-
1Can you give a little more detail about what you're trying to accomplish? What is the relationship between the rows you want to compare? If they're adjacent, then you can simply store the row in a variable, proceed to the next row, then access anything you need from the variable.– EvanJun 1, 2016 at 15:02
-
1Could you provide with a sample of the code you've tried?– TomJun 1, 2016 at 16:00
1 Answer
Set your row values to a different variable and proceed with your next iteration.
lastRow = None
with arcpy.da.UpdateCursor (fc, [field1, field2, field3]) as cursor:
for row in cursor:
if lastRow:
###perform comparision/calculation between lastRow and row
lastRow = row