[GIS] Summing column values in table using arcgisscripting

arcgis-10.1arcpypython

I need to create a python script that sums a column's values in a table. Thereafter, the script is supposed to get the percentage of each individual row in that column and populate this percentage values in a another column. I been struggling for quite some time now, I checked the net, but there is nothing of this sort. The nearest is an ArcGIS Discussion Forum thread, but its for an older version of ArcMap, or I am using it wrong.

import arcgisscripting, sys
gp = arcgisscripting.create()

intable = sys.argv[1]
field = sys.argv[2]

# Create search cursor
rows = gp.SearchCursor(intable)
row = rows.Next()
x = 0.0
# Enter while loop for each feature/row
while row:
    x += row.getvalue(field)
    print x
    row = rows.next()
#note value can be rounded depending on field type
gp.calculatefield(intable,sys.argv[3],float(x),"PYTHON")

Best Answer

try this:

ctotal = sum([row[0] for row in arcpy.da.SearchCursor("inputtable",["columnX","PercentColumn"])])#column total

with arcpy.da.UpdateCursor("inputtable",["columnX","PercentColumn"]) as cursor:
     for row in cursor:
         row[1] = (row[0]/float(ctotal))*100
         cursor.updateRow(row)