# Writing python script to group and sum records from each group? [closed]

I am doing hydrology analysis in ArcMap 10.2 and have a drainage area feature class containing hundreds of records. Each record is assigned a drainage area name and acreage amount.

How do I sum all the acreage amounts for each drainage area name using a python script preferably in the field calculator?

• gis.stackexchange.com/questions/118628/… may provide some ideas. – Richard Morgan Jan 18 '17 at 21:10
• arcpy.Dissolve_management(feature_class,outputname,[drainage_field or fields],['Acres','SUM']) – ziggy Jan 18 '17 at 21:17
• Welcome to GIS SE! As a new user be sure to take the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet (or in this case an expression and code block) in your question. There is an edit button beneath your question which will enable you to do that and a `{}` button that enables you to format any highlighted code nicely. – PolyGeo Jan 19 '17 at 5:26

If you do not necessarily need to use python to do this, you can use the Summary Statistics (Analysis) tool.

http://resources.arcgis.com/en/help/main/10.1/index.html#//00080000001z000000

Select your acreage field as the Statistics Field(s) with a Statistic Type of SUM, and select your drainage area name field as the Case Field.

• I agree - the Field Calculator is the wrong tool for trying to do this, irrespective of whether it can be done using it. – PolyGeo Jan 19 '17 at 5:25

You can do this, somewhat cumbersomely, in the field calculator like:

Parser: Python

Expression:

``````my_func(!YOUR_GROUP_FIELD!)
``````

Codeblock:

``````def my_func(group):
fc = 'YOUR_FEATURE_LAYER'
sum_field = 'YOUR_SUM_FIELD'
group_field = 'YOUR_GROUP_FIELD'