Im creating a script to calculate some new fields in Arcgis10.1 using the arcpy library. I need to sum the total of a table column in order to calculate a new field. I wanted to ask if it is possible to store all the values in an array so i can get the total sum and use the variable in the new field calculation?
summed_total = 0 with arcpy.da.SearchCursor(fc, "field to be totaled") as cursor: for row in cursor: summed_total = summed_total + row
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()