1

This question already has an answer here:

I am trying to populate Field B based on the value in Field A and the next value in Field A. I'm having trouble with the syntax for getting the "next" value in Field A. Here is my current code:

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    for row in cursor:
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        if fieldA == fieldA_next:
            counter += 1
        else:
            counter = 1
        cursor.updateRow(row)

And here's what I'm trying to produce (FieldA is known): enter image description here

marked as duplicate by FelixIP, PolyGeo arcpy Jan 7 at 2:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Grab the feature class values as a dictionary using FID as key; in the loop access the next value from the dict with row[0]+1 with appropriate checking that the key is in the dict but make sure you're not editing or the FID values are not guaranteed to be 0 based, ascending and contiguous. – Michael Stimson Jan 7 at 0:40
2

Give this a try:

# get a list of all the values of fieldA
# this ONLY works for shapefiles
NextValues = [row[0] for row in arcpy.da.SearchCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    for row in cursor:
        NextFID = row[0] + 1 # the index of the next value
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextFID < len(NextValues):
            if fieldA == NextValues[NextFID]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record

If you're using a geodatabase feature class you will need to keep a track of the index as you can't use the OID - it's not guaranteed to be 0 based, unique and contiguous - only 0 based and unique (not contiguous) so calling the next OID value may not retrieve a record:

# get a list of all the values of fieldA
NextValues = [row[0] for row in arcpy.da.SearchCursor(copyFile,'FieldA')] 

with arcpy.da.UpdateCursor(copyFile, ['OID@', 'FieldA', 'FieldB']) as cursor:
    counter = 1
    NextRow = 1
    for row in cursor:
        row[2] = counter
        fieldA = row[1]
        # get next fieldA
        # make sure this isn't the last record or there will be no next
        if NextRow < len(NextValues):
            if fieldA == NextValues[NextRow]:
                counter += 1
            else:
                counter = 1
            cursor.updateRow(row)
        else:
            # do something here for the last record
        NextRow += 1
  • perfect, thanks! only comment, with help of @FelixIP : missed an "r" in "seaRch" : NextValues = [row[0] for row in arcpy.da.SearchCursor(copyFile,'FieldA')] – AlexS1 Jan 7 at 18:35
  • Doh! I'll fix that now. – Michael Stimson Jan 7 at 22:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.