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

3741
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
DarrenWiens2
MVP Honored Contributor

Ignoring numpy for the moment, you can load your list via a SearchCursor:

def prob(Failure_Score):

  my_list = [i[0] for i in arcpy.da.SearchCursor('points','failure_sc')] # change to suit your feature class and field name

  my_max = max(my_list)

  percentage = (float(Failure_Score)/my_max)*100

  return percentage

FrankWakeley
New Contributor II

Thank you for your reply!

So inputting

def prob(Failure_Score): 

  my_list = [i[0] for i in arcpy.da.SearchCursor('PoF_Out','Failure_Score')]

  my_max = max(my_list) 

  percentage = (float(Failure_Score)/my_max)*100 

  return percentage 

I'm still getting a Null value. Will SearchCursor still work in Field Calculator even though the tool only works horizontally?

0 Kudos
DarrenWiens2
MVP Honored Contributor

Yes, SearchCursor works in the Field Calculator, although it's not very efficient because the function is run for every row in your dataset. So, for every row, the SearchCursor reads every row to load into the list. Likewise for the numpy solution, for every row, the table is transformed into a numpy array.

I think the following sort of pattern may be better, although I'm not 100% sure of the sequence - happy to have someone clarify. I believe the following reads the list once (I switched over to the numpy solution), then refers to the list inside the function:

my_list = arcpy.da.TableToNumPyArray('points','failure_sc')

my_max = max(my_list['failure_sc']) 

def prob(Failure_Score): 

  global my_max

  percentage = (float(Failure_Score)/my_max)*100 

  return percentage

It's hard to say why you're getting nulls. Are your failure scores numeric?

FrankWakeley
New Contributor II

Still no dice 😕

The scores are numeric ranging from 0.001892 all the way to 1788.782401

I'm wondering if I'm inputting a field incorrectly or something of that nature?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

why don't you include a screen shot of your field and the field calculator side-by-side so people can see... that may clarify your issue

0 Kudos
FrankWakeley
New Contributor II

Good point. It's updated now.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Here is one way that seems to work in numpy, but I'm not an expert:

def prob(Failure_Score):

  my_list = arcpy.da.TableToNumPyArray('points','failure_sc') # change for your FC and field name

  my_max = max(my_list['failure_sc'])  # change for your field name

  percentage = (float(Failure_Score)/my_max)*100

  return percentage

0 Kudos
WesMiller
Regular Contributor III

Your expression should be prob(!fieldName!)

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

first, you need to specify the field that you want to use in the field calculation, not the field you want to put it in

second, why don't you use the regular field calculator, with a python parser, with a code block, then enter your code block an put your field calculation in.   The interface you are using is generally only useful if you are using modelbuilder, not fo updating a field in a table.  So examine Darren's code and emulate within the regular field calculator,  when that works, you can copy the code snippet if you do have a need to use that tool in the environment for which it was designed

0 Kudos