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.
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.
Seems like the error is from line 1
The picture above shows the first few lines of the expression, what could have gone wrong here?
for row in cursor:
bit. That and everything below it should be indented one more level (it's inside thewith
) unless that didn't paste properly.