i'm kinda stuck with an UpdateCursor. This is what I'm trying to do: each cell of a grid stores multiple answers that were given in a survey. For each cell I must choose only ONE answer, the one that has the highest rates.

enter image description here

What I'm trying to do in my code is to select the rows that have the same value for the ID_GRID, compare the values in the PERCENT field, and update the FINAL_VALUE with the value from the VALUE field corresponding to the highest PERCENT value. In the example below, the answer 3310 has a rate of 76% > FINAL_VALUE for the selected results must be 3310.

My code takes only the last value in the SearchCursor and updates the whole FINAL_VALUE field with it:

myTable = r"D:\...."
myFields = ['PERCENT', 'VALUE']

with arcpy.da.SearchCursor (myTable, "ID_GRID") as scursor:
    for srow in scursor:
        with arcpy.da.SearchCursor(myTable, myFields, "ID_GRID" + "= " + str(srow[0])) as sscursor:
        maxValue = None
        ID = None

        for ssrow in sscursor:
            if maxValue < ssrow[0]:
                maxValue = ssrow[0]
                ID = ssrow[1]

                with arcpy.da.UpdateCursor(myTable, "FINAL_VALUE") as ucursor:
                    for urow in ucursor:
                        urow[0] = ID

1 Answer 1


You certainly could fix this (I think) but I think you should rethink the approach. Summary Statistics will do this for you and it would be easier but the downside (I suppose) is it creates new ouput. That being said you can always join it back to your original table based on the ID_GRID field (which could also be scripted). It should also be faster because you aren't looping through the layer twice.

myTable = r"D:\...."
myOutTable = r":\...."

arcpy.Statistics_analysis(myTable, myOutTable, [["VALUE", "MAX"]], "ID_GRID")

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.