[GIS] How to Populate new field using expression containing MAX value from another field in ArcGIS attribute table using field calculator

arcgis-desktopfield-calculatorpythonvbscript

I've got one field X with 10 numerical attributes (float/double).

I've created a new field Y (also float/double) which I need to populate with the percentage of each record/attribute (in each of the 10 rows) relative to the MAX value in field X, i.e. something like Y=X/max(!X!).

I need to do this using the field calculator – in VB script or Python.

Any suggestions? I thought using max before the field name would suffice? (I assumed it was a built-in-function from Python in ArcGIS).

Best Answer

You cannot use the field calculator to find the maximum value within all of the rows in a field. However, the field calculator can perform operations horizontally, among rows using functions like max([!field1!, !field2!, !field3!]).

The only way to accomplish this is by using cursors outside of the field calculator. Try the following workflow:

Use a generator to list all of the row values in X and find the list max

maximum = max(row[0] for row in arcpy.da.SearchCursor(fc, ['X']))

Then loop through each row and perform the calculations:

with arcpy.da.UpdateCursor(fc, ['X','Y']) as cursor:
    for row in cursor:
        row[1] = row[0] / maximum

--

import arcpy

fc = r'C:\path\to\your\featureclass'

maximum = max(row[0] for row in arcpy.da.SearchCursor(fc, ['X']))

with arcpy.da.UpdateCursor(fc, ['X','Y']) as cursor:
    for row in cursor:
        row[1] = row[0] / maximum
        cursor.updateRow(row)