0

I am trying to add all of the counts in an attribute table, to get the total number of counts (I am trying to figure out the percent aspect for a polygon).

My code works fine, I am not getting any errors. I just need to know how to sum the "Count" row in the attribute table. I know how to do it in ArcMap, but I don't know how to write a code for the summarize statistics. I am using ArcGis 10.3

def getaspect(outras):
    TheRows = arcpy.SearchCursor(outras)
    for TheRow in TheRows:
        pixval = TheRow.getValue("VALUE")
        pixcount = TheRow.getValue("COUNT")
        count = TheRow.getValue("COUNT")
        print count
        if pixval == 3:    
            shed = pixcount
        else:
            pass
    cellsize = arcpy.GetRasterProperties_management(outras, "CellSIZEX")
    cellsize = float(cellsize.getOutput(0))
    pixarea = cellsize * cellsize
    print "writing new watershed"
    exfile = open("C:...Myworkspace.../List.txt", "a")
    aspect = shed * pixarea
  • 2
    I used the "code" button to format your code for easier readability. Can you please clarify what the problem is - where is your code failing? There are many ambiguities in the above code sample so if you can pinpoint where you are seeing unexpected results, we may be able to suggest improvements – Stephen Lead Mar 15 '16 at 2:09
  • 3
    You have posted a function, but have not told us whether it works or not? What version of ArcGIS for Desktop are you using? If it is 10.1 or later then I recommend investigating arcpy.da.SearchCursor() in preference to arcpy.SearchCursor(). – PolyGeo Mar 15 '16 at 2:09
  • 1
    Have you considered using a tool like Summary Statistics to get your count/sum? It would be much quicker. – Michael Stimson Mar 15 '16 at 3:55
1

At some point, you should keep track of the cumulative total (using +=):

TheRows = arcpy.SearchCursor(outras)
cumulative = 0
for TheRow in TheRows:
    pixval = TheRow.getValue("VALUE")
    pixcount = TheRow.getValue("COUNT")
    count = TheRow.getValue("COUNT")
    cumulative += count # add current count to cumulative count
    # the rest of your code

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.