1

I'm trying to replicate, using arcpy, the 'right-click - Summarize' function in ArcMap. I want to specifically summarize column 'A' of my attribute table (this is a point layer), to get the count. But I also want to use column 'B' (area), and sum that for each category in A.

So far I've only found how to summarize A on it's own.

Any help?

This is the only related script I have found, but only does half the job:

def SumStats(shapeFile, fieldName):
sumDict = {}

with arcpy.da.SearchCursor(shapeFile,[fieldName]) as scurse:
    for row in scurse:
    if row[0] in sumDict:
        sumDict[row[0]] += 1
    else:
        sumDict[row[0]] = 1

tbl = arcpy.CreateTable_management("in_memory","MuchGIS")[0]

arcpy.AddField_management(tbl,"Item","TEXT")
arcpy.AddField_management(tbl,"Count","SHORT")

with arcpy.da.InsertCursor(tbl,["Item","Count"]) as icurse:
for k,v in sumDict.iteritems():
        icurse.insertRow((k,v,))
1
  • Updated - Thanks Alex, forgotten I could use that tool in model builder. Did that and exported as a script, working perfectly now. Commented Apr 10, 2018 at 13:52

2 Answers 2

2

The functionality you are looking for is already implemented within the Summary Statistics (Analysis) geoprocessing tool available in all license levels.

1

You were most of the way there. Add both fields into the cursor and increment by area.

with arcpy.da.SearchCursor(shapeFile,['A','B']) as scurse:
    for row in scurse:
        if row[0] in sumDict:
            sumDict[row[0]] += row[1]
        else:
            sumDict[row[0]] = row[1]

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.