3

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

6
  • 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
    Commented Feb 7, 2014 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
    Commented Feb 7, 2014 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
    Commented Feb 7, 2014 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
    Commented Feb 7, 2014 at 13:25
  • I am a beginner in python. For calculating maximum value from numeric field I need use this part of code: maximum = max(row[0] for row in arcpy.da.SearchCursor(fc, ['X'])) But after insertion into the field calculator an error message appears. In field calculator I chose python parser. Any solution? Thank you
    – user
    Commented Feb 22, 2017 at 16:07

3 Answers 3

4

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

I found a solution using arcpy.da.TableToNumPyArray in the Field calculator using Python. It first creates an Array (list) of all the 'X' values. Afterwards the max value is calculated and given a seperate variable. Last step is to divide this maxium value from each 'Y' value ('Scores', in my example below).

def my_rank(score_value):
    my_list = arcpy.da.TableToNumPyArray ("Rank_test", "Scores")
    my_max = my_list["Scores"].max()
    ranking_score = score_value / my_max 
    return ranking_score
1
  • sorry for the late comment. this works great also, thanks!!
    – GeoLuCa
    Commented Jan 29, 2016 at 10:30
0

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.

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