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

4629
21
04-20-2016 11:37 AM
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!

21 Replies
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```
New Contributor II

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?

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?

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?

MVP Legendary 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

New Contributor II

Good point. It's updated now.

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```
Regular Contributor III