Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

UpdateCursor based on results from SearchCursor

$
0
0

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
                        ucursor.updateRow(urow)

Viewing all articles
Browse latest Browse all 767

Trending Articles