1

I am trying to get field values from one table and use them to perform calculations on a row in another fc table. Does anyone have any ideas on how to make this run faster? I've tried a dictionary but am having problems when i try to reassign a value in the dictionary.

import arcpy
import numpy as np


arcpy.env.workspace = "C:\Users\michellep\Documents\project.gdb"

print "Arcpy & Numpy imported successfully and workspace set"
print "Start Processing"
start = time.time()
#define variables
group = "Test_Group"
fields = ['ObjectID', 'R', 'A', 'Total']
pc = "PC_Table"

cursor = arcpy.da.UpdateCursor(group, fields)
    #row = cursor.next()
for row in cursor:
    R = row[0]
    A = row[1]
    arcpy.CalculateField_management(pc, "R", R, "PYTHON", "")
    arcpy.CalculateField_management(pc, "A", A, "PYTHON", "")
    arcpy.CalculateField_management(pc, "WVal", "!R! / !A! * (!B! / !A!)**(!R!-1), "PYTHON", "")
    arcpy.CalculateField_management(pc, "Hours", "!WVal! * !K!", "PYTHON", "")
    arr = arcpy.da.TableToNumPyArray(pc, "Hours")
    arr.dtype = np.dtype(float)
    row[2] = np.sum(arr)
    cursor.updateRow(row)
    del row

del cursor
end = time.time()
processTime = end - start
print "Processing Completed - {:.1f} seconds".format(processTime)
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. – Midavalo Apr 4 '17 at 15:43
  • You appear to be missing a quote mark " in your third CalculateField line. – Midavalo Apr 4 '17 at 15:44
  • 4
    Why are you using Calc Field inside a for loop? Your not changing the selected rows, so it's always running on the entire table each and overwriting the previous iteration. – Paul Apr 4 '17 at 16:01
1

It looks like you're trying to update each row using calculate field. Calculate field performs the calculation on the entire table. Something like this is probably what you're looking for:

arcpy.env.workspace = "C:\Users\michellep\Documents\project.gdb"

print "Arcpy & Numpy imported successfully and workspace set"
print "Start Processing"
start = time.time()
#define variables
group = "Test_Group"
fields = ['ObjectID', 'R', 'A', 'Total']
pc = "PC_Table"

cursor = arcpy.da.UpdateCursor (group, fields)
for oid, r, a, total in cursor:
    total = (r * a) / (B / a) ** (r * -1)
    row = (oid, r, a, total)
    cursor.updateRow(row)

Note: variable B is not defined in the script.

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.