Need Python Example: sort rows, subtract current from next, load in new field

6668
18
04-21-2011 06:18 AM
NatalieLepsky
New Contributor
Please help! I am new to Python.

I created a model in ModelBuilder. The last steps are to grab the attribute table of the feature class, sort MEAS column in ascending order and then subtract next value from current and load those values in the new field. (I have an empty field in the attribute table).

I wrote a simple script, but all I could do is to sort the MEAS column and print values out.
Could anybody be so kind and get me an example code?....
Tags (2)
0 Kudos
18 Replies
DarrenWiens2
MVP Honored Contributor
You should use an UpdateCursor (there's an example). In the UpdateCursor statement, you specify which fields to sort. Then you loop through all your rows, each time sticking the current value into a variable to save it for the next row where you update the empty column with that value.
0 Kudos
NatalieLepsky
New Contributor
Thanks for your reply! I created a new dataset with sorted rows using a simple script. But still have problems with UpdateCursor. I try to retain the current value, but nothing works. Do you have a similar looping example?
0 Kudos
TerrySilveus
Occasional Contributor III
put the value into a variable for later use.  i.e. air code below

firstTime = True
for row in rows:
   if firstTime:
      myMeas = row.getValue('MEAS') #in your first iteration you will not have anything to update you just need to get the value for the next iteration
      firstTime = False
   else:
      row.setValue('MEASCalc', row.getValue('MEAS') - myMeas
      rows.updateRow(row)
      myMeas = row.getValue('MEAS')  #get the value for the next iteration 
 

NOTE: I fairly unfamiliar with python so you may have to tweak the syntax to make this work
0 Kudos
DarrenWiens2
MVP Honored Contributor
rows = UpdateCursor(...)

count = 0

for row in rows:
    if count != 0: # if this isn't the first time in the loop (ie. if you have a number to subtract)
        count = count + 1
        row.blankcolumnname = row.MEAS - previous # make the blank column = current - previous
        rows.updateRow(row) # updates the whole row, now with a value for the blank column
        previous = row.MEAS # sticks the current value into the previous variable for the next time through the loop

del row
del rows # remove row locks
0 Kudos
NatalieLepsky
New Contributor
Thanks so much!
But I ru the code I get an error: 'module' object has no attribute 'Update'
What would that be?
0 Kudos
DarrenWiens2
MVP Honored Contributor
start with:
import arcpy

and the actual update cursor statement should be like:
rows = arcpy.UpdateCursor(FILL IN YOUR PARAMETERS HERE)
0 Kudos
NatalieLepsky
New Contributor
Fixed a typo and got no more errors. Thanks! But the field in my table didn't get updated...
I ran exactly what you sent me. What else could it be?

# Import the arcpy
import arcpy
# Input feature class.
rows = arcpy.UpdateCursor("C:/model_build/ModelBuildGeodatabase.mdb/locate_feature_layer_sort")
count = 0
for row in rows:
    if count != 0:
        count = count + 1
        row.MEAS_DIST = row.MEAS - previous
        rows.updateRow(row)
        previous = row.MEAS
del row
del rows
0 Kudos
TerrySilveus
Occasional Contributor III
It probably won't update if the table is open...
I tested this code below and it works.

import arcpy

myFile = "c:/gis/temp.shp"
sortField = "MEAS"
newField = "NEW"

rows = arcpy.UpdateCursor(myFile,"","","",sortField + " A") # sort by sort field
firstTime = True
for row in rows:
   if firstTime:
      previous = row.getValue(sortField) #in your first iteration you will not have anything to update you just need to get the value for the next iteration
      firstTime = False
   else:
       row.setValue(newField, row.getValue(sortField) - previous)
       rows.updateRow(row)
       previous = row.getValue(sortField)  #get the value for the next iterationexcept:

del row
del rows
0 Kudos
DarrenWiens2
MVP Honored Contributor
Oops - in my example I never gave it any commands for what to do if it was the first time around the loop, so the counter stayed at 0 and never got into the update part. Here's what it should be (you could also add an updateRow to the else part if you want the very first row to have a value - this way it will just be skipped over).

for row in rows:
    if count != 0: # if this isn't the first time in the loop (ie. if you have a number to subtract)
        count = count + 1
        row.blankcolumnname = row.MEAS - previous # make the blank column = current - previous
        rows.updateRow(row) # updates the whole row, now with a value for the blank column
        previous = row.MEAS # sticks the current value into the previous variable for the next time through the loop
    else: # do this if it is the first time in the loop
        count = 1
        previous = row.MEAS
0 Kudos