I am new to scripting. I have been searching around on here and other sites, and can't seem to find something quite like what I need.

I am trying to find a max value within a field, and store that max value as a variable to get used for other processes later. I think I am defining the variable incorrectly, but I can't seem to find anything on how to define the max value of a field as a variable.

Here is what I have so far.

In this code, centerline is a variable that grabs a user defined feature class (this code is being set up as an Arc tool). SAMPINS is the name of the field that I am trying to grab the max value for. I realize that I can use statistics to get the max value (and maybe that is a better option here...), as well, but I still do not know how to define this as a variable.

The traceback error is telling me that addfields is not defined on line 179. Line 179 is the line that reads "addfields - 1 < [SAMPINS]".

addfields = 0
fieldtag = str(addfields)
with arcpy.da.SearchCursor(centerline, "SAMPINS") as cursor:
    for row in cursor:
        value = row[0]
        if value > addfields:
            addfields = value

while (addfields > 1):
    arcpy.AddField_management(centerline, "F_"+fieldtag, "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    if addfields:
        addfeilds - 1 < [SAMPINS]
        arcpy.CalculateField_management(centerline, "F_"+fieldtag, "[SAMPINS] + [SAMPDIST] + [SAMPLF]", "VB", "")
        arcpy.CalculateField_management(centerline, "F_"+fieldtag, "0", "VB", "") 
    addfields = addfields - 1

You code seems pretty close:

max_value = -99999999
with arcpy.da.SearchCursor(centerline, "SAMPINS") as cursor:
    for row in cursor:
        max_value = max(int(row[0]), max_value)
# do something with max_value

Note: using GeoSharp's idea below of using:

max_value = float('-inf')

is the most bulletproof way of dealing with negative data values

  • No need to initialize a value at all: max_value = max(value for value, in cursor) – mikewatt Oct 9 at 19:46
  • Slick, but maybe not the best advice for someone "new to scripting". – Marc Pfister Oct 11 at 22:15

With this code snippet, once the loop is completed the variable max_value will contain the maximum value of the column in question:

max_value = float('-inf')
input_feature_class = r'C:\path\to\feature_class'
field_to_get_max_value_for = "SAMPINS"
with arcpy.da.SearchCursor(input_feature_class , field_to_get_max_value_for) as cursor:
    for row in cursor:
        value = row[0]
        if value > max_value:
            max_value = value

It is best to start by using the absolute minimum value of a float in Python to start with, e.g. float('-inf'). You are essentially guaranteed to have the first value in search cursor loop to be greater than it. From there if a value is ever greater than the first value in the column it will replace the value for max_value and continually do this until the loop is complete.

  • Thanks a lot GeoSharp! This seemed to helped get past that initial road block, but it seems that the max_value variable does not continue through? Maybe the while loop needs to be situated underneath the max value grab? – rafbas Sep 5 '17 at 18:40

If I understand your question, you are looking for the max value in a field called "SAMPINS"...

Therefore the way ive done this before is to use a list, append all the values of SAMPINS to the list, then find the max value for it.

Therefore try this:

list = []
with arcpy.da.SearchCursor(centerline, "SAMPINS") as cursor:
    for row in cursor:
max_value = max(list)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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