1

I am trying to find the field with highest area value in each row of the 5 fields. Now I have found the highest value. But it doesn't really do me any good if I couldn't find which field is that max.

enter image description here

I have some rudimentary understanding and practice of Python. The very similar answer here:

need to find max value among fields AND PRINT FIELD NAME

When I applied the code, it always gave me a failure, which is probably due to my lack of understanding of Python.

Error 000539, Python syntax.


The code I adapted from that question is shown as below:

import arcpy

table = "L:\......\ParcelSummary.dbf"
fields = [ !SUM_acres_! , !SUM_acres1! , !SUM_acre_1! , !SUM_acre_2! , !SUM_acre_3! ]
n = len(fields)


maxfield = "Highest"
maxname = "HighestF"
arcpy.AddField_management(table, maxfield, "FLOAT")
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:

    check = row[:n]
    maxval = max(check)

    row[-2:] = maxval, fields[check.index(maxval)]

    cursor.updateRow(row)

Also, a pretty basic question about this: shall I put the code into the Field Calculator expression or shall I put it in Pre-Logic Script Code? If I put them in the Codeblock, then what shall my expression be?

As for the field names, the ones shown in the picture are the aliases of the fields instead of names.


My exact process and the error message I got.

Field Calculator Error Message

Seems like the error is from line 1

enter image description here

The picture above shows the first few lines of the expression, what could have gone wrong here?

  • What is the actual error or behavior? It looks like this should work, minus the fact that you are loading in the XML metadata file instead of the actual DBF table... – Evil Genius Sep 21 '16 at 14:11
  • Error 000539, Python syntax. The xml path was like that when I copied the path, just corrected it. No idea what actually went wrong. I should put the code in the expression right? Thanks – AndrewLebron Sep 21 '16 at 14:21
  • 1
    What line is the syntax error referring to? At a guess, it's probably the for row in cursor: bit. That and everything below it should be indented one more level (it's inside the with) unless that didn't paste properly. – Evil Genius Sep 21 '16 at 14:25
  • 2
    How are you executing this? If you're not in Field Calculator you don't want the exclamation points around the field names they will need to be strings. – Brian Sep 21 '16 at 14:35
  • 1
    Where is the CalculateField line in your code sample? – jbchurchill Sep 21 '16 at 15:40
3

This should work:

def compMaxField(table, fields, maxFldName, maxValName):
  arcpy.AddField_management(table, maxFldName, 'TEXT') # field name with max value
  arcpy.AddField_management(table, maxValName, 'FLOAT') # the max value
  with arcpy.da.UpdateCursor(table, fields + [maxFldName, maxValName]) as cur:
    for row in cur: # everything inside a context must be indented
      maxVal = max(row[:-2]) # get the max value, skip the output fields
      maxFld = fields[row.index(maxVal)] # find out the corresponding field name
      # make a new row
      row = list(row[:-2]) + [maxFld, maxVal]
      cur.updateRow(row)

Place this into the pre-logic section, then just call it from the calculator like this:

compMaxField("L:\\......\\ParcelSummary.dbf",
             ['SUM_acres_' , 'SUM_acres1', 'SUM_acre_1', 'SUM_acre_2', 'SUM_acre_3'],
             'MaxArea', 'MaxField')

As @Florjan said, the backslashes in the path should be double-escaped, or you can use a raw string with the r prefix.

  • Small adjustment, should be for row in cur: not cursor – Brian Sep 21 '16 at 14:43
  • @AndrewLebron Check the 'Show Codeblock' checkbox in the Field Calculator window. This will open the Pre-Logic Script Code window. Then try using the above code provided by Jan. – Brian Sep 21 '16 at 15:30
  • Updated, it should work now, accounting for both errors. Thanks @Brian! – Jan Šimbera Sep 22 '16 at 8:11
  • Thank you so much! Weird that it worked today not yesterday considering I did the same thing. Anyway, thanks a ton! – AndrewLebron Sep 22 '16 at 12:57
1

Before the dbf-file insert "r" letter:

table = r"c:\.....\name.dbf"

Change the expressed field names and if you take any error share it:

fields = [ "SUM_acres_" , "SUM_acres1" , "SUM_acre_1" , "SUM_acre_2" , "SUM_acre_3" ]

  • Thank you. Changed the path and quotation marks but still get error message in line 1. Just edited the original post with the expression I have in Field Calculator. – AndrewLebron Sep 21 '16 at 14:58
  • You script with my suggestions should be run from python terminal not to field calculation. If you wanna try the field calculation method get the @jan reply, he has adapted it for field calculation. – Florjan Sep 21 '16 at 15:03
  • using field calculation forget my reply, check "code block" and copy and paste Jan code. Separately first block paste it to "Pre-Logic" space and second block paste it to last space. – Florjan Sep 21 '16 at 15:10
  • i cannot help for Jan part cause never used searchcursor on field calculation, i have to test it for myself ^_^ – Florjan Sep 21 '16 at 15:12

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.