0

Beginner with Python trying to perform multiple calculations on a queried set of rows. I'm am then looping thru a set of queries. But I need to perform all calcuations on returned rows before starting next query in the array. The first set of if else statements runs correctly. The subsequent statements don't update every record. Here's what I have so far...

for fl in flArray:
calcFields = ['FL', fl, 'EValue', 'Diff', 'OOC', 'E_GRTR', 'gCAT', 'eCAT']
queryFL = flArray[fl]
cursor = arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL)

 # Calculate difference    
 for row in cursor:
  row[3] = row[2] - row[1]
  if row[3] > 0:
        row[5] = 1
  else:
        row[5] = 0
  cursor.updateRow(row)

 ### Calculate g Category

 for row in cursor:
  if row[1] < .15:
     row[6] = 0
  elif .15 <= row[1] <= .22:
     row[6] = 1
  elif .23 <= row[1] <= .3:
     row[6] = 2
  elif .31 <= row[1] <= .37:
     row[6] = 3
  elif .38 <= row[1] <= .45:
     row[6] = 4
  elif row[1] > .45:
     row[6] = 5
  cursor.updateRow(row)

 ### Calculate e Category

 for row in cursor:
  if row[2] < .15:
     row[7] = 0
  elif .15 <= row[2] <= .22:
     row[7] = 1
  elif .23 <= row[2] <= .3:
     row[7] = 2
  elif .31 <= row[2] <= .37:
     row[7] = 3
  elif .38 <= row[2] <= .45:
     row[7] = 4
  elif row[2] > .45:
     row[7] = 5
  cursor.updateRow(row)

 ### Calculate OOC

 for row in cursor:
  row[4] = abs(row[6] - row[7])      
  cursor.updateRow(row)
  cursor.reset
  • Multiple elif can be squeezed gis.stackexchange.com/questions/177652/… – FelixIP Jan 15 '17 at 22:04
  • Values in the range (0.22,0.23), and in all the other gaps, will not have any values set. This is poor coding practice. – Vince Jan 15 '17 at 22:13
  • @Vince I fixed ranges to include the gaps. Thanks for catching this – Jotter Jan 18 '17 at 17:39
0

This is not highly Pythonic but as a quick fix try the following. Rather than trying to backup using the reset method, simply run through the table four times. By using with syntax there is no need to clean up the row and cursor objects when each cursor completes.

for fl in flArray:
calcFields = ['FL', fl, 'EValue', 'Diff', 'OOC', 'E_GRTR', 'gCAT', 'eCAT']
queryFL = flArray[fl]

 # Calculate difference    

with arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL) as cursor:
 for row in cursor:
  row[3] = row[2] - row[1]
  if row[3] > 0:
        row[5] = 1
  else:
        row[5] = 0
  cursor.updateRow(row)

 ### Calculate g Category    
with arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL) as cursor:
 for row in cursor:
  if row[1] < .15:
     row[6] = 0
  elif .15 <= row[1] <= .22:
     row[6] = 1
  elif .23 <= row[1] <= .3:
     row[6] = 2
  elif .31 <= row[1] <= .37:
     row[6] = 3
  elif .38 <= row[1] <= .45:
     row[6] = 4
  elif row[1] > .45:
     row[6] = 5
  cursor.updateRow(row)

 ### Calculate e Category
with arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL) as cursor:
 for row in cursor:
  if row[2] < .15:
     row[7] = 0
  elif .15 <= row[2] <= .22:
     row[7] = 1
  elif .23 <= row[2] <= .3:
     row[7] = 2
  elif .31 <= row[2] <= .37:
     row[7] = 3
  elif .38 <= row[2] <= .45:
     row[7] = 4
  elif row[2] > .45:
     row[7] = 5
  cursor.updateRow(row)

 ### Calculate OOC
with arcpy.da.UpdateCursor(out_layer_file, calcFields, queryFL) as cursor:
 for row in cursor:
  row[4] = abs(row[6] - row[7])      
  cursor.updateRow(row)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.