I was wondering if an equivalent of summary statistics exists for finding such statistics across fields rather than records in a table. For example, of several fields, I wanted to find which one has the highest value for a particular record, and its name. Here, I found a cursor for doing so, but keep running into a problem.

I made few alterations to that cursor, apart from specifying the fields I want to run it on (I have ten such fields, so n = 10) and changing the format and names of some of them where necessary. Below is my version of this cursor:

import arcpy

# input data and fields to find maximum
table = '[the path to my file]'
fields = ["B0R001", "B0R002", "B0R003", "B0R004", "B0R005", "B0R006",   "B0R007", "B0R008", "B0R009", "B0R0010"]
n = len(fields)

# Add fields to input table to store maximum and field name
maxfield = "HIGHEST_M"
maxname = "HGHSTMNAME"
arcpy.AddField_management(table, maxfield, "LONG")
arcpy.AddField_management(table, maxname, "TEXT")

fields2 = fields[:] # shallow copy
fields2.extend([maxfield, maxname])

with arcpy.da.UpdateCursor(table, fields2) as cursor:
for row in cursor:

    # Look at the first n values        
    check = row[:n]
    maxval = max(check)

    # Get the index position of the maxval and use it slice into field list
    # which gives us the field name
    # only update the last two rows
    row[-2:] = maxval, fields[check.index(maxval)]


print "Done!"

The script keeps throwing an error, however. The message is as follows:

Traceback (most recent call last): File "[Path to my File]", line 18, in for row in cursor: RuntimeError: A column was specified that does not exist.

Am I right that the "column" refers to the column in which the cursor should start checking maximum values? My 10 columns do not begin with column one. Where in the script should I have specified the column in which the cursor starts looking for maximum values? If I want the cursor to search, for each record, for maximum values only in, say, columns 20 through 29, inclusive, what should I do differently? Sorry about this, I haven't written cursors in a while.


1 Answer 1


I'm going to go ahead and convert my comment to an answer. I'm assuming you've named your last field incorrectly. I think you may have meant it to be "B0R010" and not "B0R0010".

Also, you may need to ensure that all fields are in the file. Also, check your indentation, as per @Jacob F's comment.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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