HOW TO

Calculate the sum of all numeric attribute fields values using ArcPy in ArcGIS Pro

Last Published: April 7, 2023

Summary

In ArcGIS Pro, the sum() function in the Calculate Field tool can be used to calculate the sum for each record from a list of numeric fields. However, when the feature class or table contains a large number of fields, it is impractical to manually select all the numeric fields.

The ArcPy script provided in this article can be used to calculate the sum for the values of all numeric fields in a feature class or table.

In this example, there are five numeric fields in the 'Point1' feature class, as shown in the next image.

The attribute field of the Point1 feature class in ArcGIS Pro.

Procedure

  1. In ArcGIS Pro, on the Analysis tab, click the down arrow next to Python, and click Python Window.
  2. In the Python window, enter the Python script below. Replace <path> with the path to the feature class or the table.
import arcpy

# Set the path to the feature class or the table
table = r""

# Add a new field to the table
arcpy.management.AddField(table, "SumField", "DOUBLE")

# Use UpdateCursor to iterate through the rows of the table
with arcpy.da.UpdateCursor(table, "*") as cursor:
    for row in cursor:
        # Initialize a variable to store the sum
        total = 0
        # Iterate through each field in the row
        for i in range(len(row)):
            # Check if the field is numeric and not OBJECTID, Shape_Length, and Shape_Area
            if isinstance(row[i], (int, float, complex)) and cursor.fields[i] not in ['OBJECTID', 'Shape_Length', 'Shape_Area']:
                # Add the field's value to the total
                total += row[i]
        # Update the new field with the total
        row[cursor.fields.index('SumField')] = total
        cursor.updateRow(row)
  1. Place the cursor at the end of the script and press Enter twice.

The sum of the numeric fields for each feature is now populated in the new field, 'SumField'.

The attribute field of the Point1 feature class with the new field containing the result of the ArcPy script.

Article ID:000029132

Software:
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic