How to get sum of table column values into Python variable?
summed_total = 0
with arcpy.da.SearchCursor(fc, "field to be totaled") as cursor:
for row in cursor:
summed_total = summed_total + row[0]
Something like this would work. Replace what's in quotes with your field name, or with a list of fields you're going to be working with. Replace fc with the feature that holds the field.
That won't work for 10.0 or earlier; da.SearchCursor didn't show up until 10.1. For earlier versions:
summed_total = 0
field = "field to be summed"
with arcpy.SearchCursor(fc):
for row in cursor:
summed_total = summed_total + row.GetValue(field)
You can accomplish this in two lines when working with numpy
arrays (TableToNumPyArray).
import arcpy, numpy
fc = r'C:\path\to\your\fc'
# Convert to numpy array. "test" is the field name
field = arcpy.da.TableToNumPyArray (fc, "test", skip_nulls=True)
sum = field["test"].sum()