HOW TO

Calculate the percentage of a field value from a specified field in ArcGIS Pro

Last Published: March 3, 2023

Summary

In ArcGIS Pro, Python expressions can be used to calculate the percentage of a field value from a specified field. This article provides an example and instructions using Python expressions to calculate the percentage of the population for each district in Hong Kong and create a new field in ArcGIS Pro.

The image below shows the PopDis2021 attribute table of the population for each district in Hong Kong.

The PopDist2021 attribute table

Procedure

  1. In the Contents pane, right-click the feature class and click Attribute Table to open the attribute table of the feature class.
Opening the attribute table of the feature class from the Contents pane
  1. Click Calculate to open the Calculate Field tool.
Note:
Refer to ArcGIS Pro: Calculate Field (Data Management) for more information on the parameters in the Calculate Field tool.
  1. In the Calculate Field dialog box, configure the following parameters:
    1. For Input Table, select the feature class.
    2. For Field Name (Existing or New), select the field to be edited or create a new field. In this example, the 'Percentage' field is created.
    3. For Field Type, select an integer field type. In this example, 'Double (64-bit floating point)' is selected.
    4. For Expression Type, select Python 3.
    5. Specify the following expression in the first code block. In this example, '<fieldName1>' is named as Number_of_Persons.
update(!<fieldName1>!)
  1. Specify the following Python expression in Code Block:
    1. Define the update() method. In this example, '<varName>' is named as numbPersons.
def update(<varName>):
Note:
The following procedure must all be within the same indented code block. Refer to the full script sample for clarification.
  1. Import the ArcPy module and create an empty array parameter.
     import arcpy    
     list = []
  1. Create a loop to run the SearchCursor() function to insert all field into the array parameter in Step 3(f)(ii).
    with arcpy.da.SearchCursor(r"<featureClassPath>", ["<fieldName1>"]) as cursor:
        for row in cursor:
            list.append(row[0])
  1. Calculate the sum of the value. In this example, '<varName1>' is named as S.
    <varName1> = sum(list)
  1. Specify the following statement to return the percentage of the field value.
    return <varname> / <varname1> * 100

The code block below is an example of the full expression.

def update(numbPersons):
    import arcpy
    list = []
    with arcpy.da.SearchCursor(r"C:\Users\ISC-Testing\Documents\29398 calculate percentage\29398 calculate percentage.gdb\PopDist2021", ["Number_of_Persons"]) as cursor:
        for row in cursor:
            list.append(row[0])
    S = sum(list)
    return numbPersons / S * 100
  1. Click Verify to run a test of the expression. If the expression is valid, click OK.
The Calculate Field tool pane with the parameters to be filled

The image below shows the 'Percentage' field added to the 'PopDist2021' attribute table.

The PopDist2021 attribute table with the Percentage field added

Article ID: 000029398

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

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options