HOW TO
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.
Note: Refer to ArcGIS Pro: Calculate Field (Data Management) for more information on the parameters in the Calculate Field tool.
update(!<fieldName1>!)
def update(<varName>):
Note: The following procedure must all be within the same indented code block. Refer to the full script sample for clarification.
import arcpy list = []
with arcpy.da.SearchCursor(r"<featureClassPath>", ["<fieldName1>"]) as cursor: for row in cursor: list.append(row[0])
<varName1> = sum(list)
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
The image below shows the 'Percentage' field added to the 'PopDist2021' attribute table.
Article ID: 000029398
Get help from ArcGIS experts
Download the Esri Support App