0

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)]

    cursor.updateRow(row)

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
    For starters, your for row in cursor: line, and everything within the subsequent loop needs to be indented. – Jacob F Jun 22 '16 at 3:50
  • 2
    Are all fields that you specify actually in your file? I just noticed that your fields are named "B0R001", "B0R002", ... , "B0R0010". Is that last one actually supposed to be B0R0010 or B0R010? – Fezter Jun 22 '16 at 3:59
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.

  • Yes, indeed, it should have been B0R010 and not B0R0010. B0R0010 was the "column that doesn't exist." I've changed that part and everything worked. Sorry. – Mhoram Jun 23 '16 at 0:50
  • No worries. Sometimes it's hard to see that sort of thing in your own code. But, the error that you got prompted me to check your fields. – Fezter Jun 23 '16 at 0:52

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.