Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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).

share|improve this question

migrated from stackoverflow.com Mar 24 at 16:23

This question came from our site for professional and enthusiast programmers.

    
Please show your code and explain what isn't working as you expected. Do you get an error? If so, what is it? –  Ansgar Wiechers Feb 7 at 9:40
    
If I use the code above with Python script Y=!X!/max(!X!) it gives me 1 as the result for all the fields as it replaces !X! with the numerical value for each respective row (basically dividing with itself rather than using the max value in field X as the denominator. –  GeosDK Feb 7 at 13:19
    
If I use VB Script it throws an error saying "There was a failure during processing, check the Geoprocessing Results window for details" (i.e. Error 999999 Error exectuing function). I've tried adding commas - different sorts -, checking spaces etc. Nothing works. –  GeosDK Feb 7 at 13:24
    
Eventually I resorted to looking for the max value by ascending the values in field X and using that value to the denominator in the field calculator. However, I would still like to find out whether it would be possible for the field calculator to populate a field automatically by searching the cursor for the max value (I know this is possible when looking for max values in multiple fields, but can't find the same for a single field) –  GeosDK Feb 7 at 13:25

2 Answers 2

up vote 1 down vote accepted

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)
share|improve this answer
1  
I suspected this was the case (not able to find the max value horizontally); your solution works perfectly, thanks. –  GeosDK Aug 10 at 22:27

If there is no other more direct way you could create an empty list and populate it with x field, maximum in a list is then easily found and used to populate the y fields.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.