Select to view content in your preferred language

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

6183
21
04-20-2016 11:37 AM
FrankWakeley
Deactivated User

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
FrankWakeley
Deactivated User

So using the regular Field Calc like I've been, I've tried substituting everything I could think of into this equation. I may just leave it at using a static number as the max for now.

2016-04-20 16_08_55-1 - HP Probability of Failure.jpg

0 Kudos
DanPatterson_Retired
MVP Emeritus

the expression box says ... prob ... you have to provide the field name !thefieldname! in ! marks

0 Kudos
DarrenWiens2
MVP Honored Contributor

Here is a working example with my feature class and field names. Pay very close attention to where there are (and aren't) exclamation points.

AbdullahAnter
Honored Contributor

You can put  Summary Statistics tool and operate this for your field to get maximum value , then you can get this value by using Get Field Value tool , and use this Value in your expression.

if you put the tools in correctly way, the value will be generate dynamically each iteration

0 Kudos
curtvprice
MVP Esteemed Contributor

I think the best way to do this Model Builder is to use Calculate Value to find the total number (or Summary Statistics connect to Get Field Value) and then pass that into your function inside Calculate Field.

curtvprice
MVP Esteemed Contributor

Here's a very fast way to get that maximum value:

Calculate Field

Expression

cfmax(r"%Input table%", "%Field%")

Code block

def cfmax(tbl, fld, where_expr=""):
  return sorted(arcpy.da.SearchCursor(tbl, fld, where_expr), reverse=True)[0][0]

Data Type

Variant

DarrenWiens2
MVP Honored Contributor

That is a straight forward method to get the max, but not very fast (perhaps you meant fast, like, quick and dirty). The cursor is created, stored, and sorted for every row in the table. Move the cursor outside the function to run only once.

Example, using 120000 features:

Expression:

cfmax()

Code Block:

def cfmax():  
  max = sorted(arcpy.da.SearchCursor("Pemberton_VRI", "POLYGON_AR"), reverse=True)[0][0] 
  return  max

Result: I ended the process after 5 minutes. Never finished.

vs.

Expression:

cfmax()

Code Block:

max = sorted(arcpy.da.SearchCursor("Pemberton_VRI", "POLYGON_AR"), reverse=True)[0][0] 
def cfmax():  
  global max  # may not be necessary
  return  max

Result: finished in 38s.

DanPatterson_Retired
MVP Emeritus

run your numpy incarnation now that you have the data Darren (ie the one with tabletonumpyarray outside of the def), I suspect most of the time will be on the conversion but it is hard to test field calculator nippets

0 Kudos
DarrenWiens2
MVP Honored Contributor

This runs in about 40s, so similar to the cursor:

my_list = arcpy.da.TableToNumPyArray('Pemberton_VRI', 'POLYGON_AR')
my_max = max(my_list['POLYGON_AR'])
def cfmax(): 
  global my_max 
  return  my_max

...and this doesn't get past 0% (a blue sliver did just appear in the progress bar after a couple minutes):

def cfmax(): 
  my_list = arcpy.da.TableToNumPyArray('Pemberton_VRI', 'POLYGON_AR')
  my_max = max(my_list['POLYGON_AR'])
  return  my_max
DanPatterson_Retired
MVP Emeritus

Thanks for running that Darren.

First incarnation is good, the second... as you know... it is doing the conversion to array for every record.... it would have been to watch memory during that one

0 Kudos