2

enter image description here

I try to sum all record in "AREA" field and put a value in "SUM" field for calculate a "Ratio" field by using Python.

But I struck in a sum part.

Here is my code :

arcpy.AddField_management(outclip, "AREA", "DOUBLE", 2, "", "", "", "", "")
arcpy.AddField_management(outclip, "SUM", "DOUBLE", 2, "", "", "", "", "")
arcpy.AddField_management(outclip, "Ratio", "DOUBLE", 2, "", "", "", "", "")

arcpy.CalculateField_management(outclip ,'AREA','!shape.area@SQUAREKILOMETERS!','PYTHON')
#arcpy.CalculateField_management(outclip ,'SUM', ['AREA', 'SUM'])','PYTHON')
0

1 Answer 1

2

You can use cursors like this:

import arcpy

fc = r'C:\folder\data.gdb\featureclass' #Change to your data source
fields = ['AREA','SUM','Ratio']

totsum = sum([row[0] for row in arcpy.da.SearchCursor(fc, fields[0])]) #List all values in area field and sum them

#Update/calculate sum and ratio fields
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        row[1] = totsum #[1] is index of SUM field in fields list
        row[2] = row[0]/totsum #[0] is AREA, [2] RATIO
        cursor.updateRow(row)

(You dont need the SUM field)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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