Is there a way to get the maximum value of a column in Field Calculator?

5366
21
04-20-2016 11:37 AM
FrankWakeley
New Contributor II

I want to have a function in my model that converts an arbitrary value to a percentage within that column's range. It would need to be dynamic. Right now I have:

def prob( Failure_Score 😞

   my_list = arcpy.da.TableToNumPyArray ("Failure_Score")

   my_max = max(my_list)

   percentage = (Failure_Score / my_max)*100

   return percentage

but it returns nothing. Really any advice would be greatly appreciated.

Attached is a screencap with the two fields in question and the Calculate Field window.

Thanks!

0 Kudos
21 Replies
JoshuaBixby
MVP Esteemed Contributor

The Python sorted function returns "a new sorted list from the items in iterable," which means the entire iterable will be consumed.  The sorted function does use an incredibly efficient sorting algorithm, so it will be faster than someone trying to role their own, but the entire data set will be touched.  Since the entire data set needs to be touched, using the Python max function achieves the same end:

def cfmax(tbl, fld):
    fld_max, =  max(arcpy.da.SearchCursor(tbl, fld))   
    return fld_max

If you are using an enterprise database, relying on the database engine to process the information on the server instead of pushing all of the data to the client would likely perform the best.

def cfmax(tbl, fld):
    sql = "ORDER BY {} DESC".format(fld)
    fld_max, = next(arcpy.da.SearchCursor(tbl, fld, sql_clause=(None,sql)))
    return fld_max
DanPatterson_Retired
MVP Emeritus

better still, do that whole bit inside a script sending everything to numpy, do the calculations and send a new file back... OR .. don't forget there is ExtendTable—Help | ArcGIS for Desktop which is arcpy interface equivalent to numpy recfunctions append_fields so you have an join option