Convert Excel formula to Python 2022

01-24-2022 01:49 AM
New Contributor

Hello everybody, i would like to be helped form you for a excel formula , that i need some help to convert to python code for GIS attributes.

I have this excel formula """=IF(A2<>A1,1,B1+1)"""" that give valued number from 1,2,3,4, etc to Vertec column when the other row value change.

For example when the object_ID change form 5/75 to another value the numer starts counting form the 1,2,3,4,5 etc until another Object_id changes again.

Photo attach.





Tags (3)
0 Kudos
1 Reply
by Anonymous User
Not applicable

You need to use a cursor.  Something like this, but keep in mind that it only works if your object_id's are grouped like in your image. they are not, you can use an order by sql clause in the cursor to group them in the desired order.

previous_object_id = None
previous_vertex_val = 1

with arcpy.da.UpdateCursor(fc, ["object_id", "Vertex"]) as cursor:
    for row in cursor:
        # check if the current object_id is the same as the previous.
        if str(row[0]) == str(previous_object_id):
            # set vertex to current vertex value
            row[1] = previous_vertex_val
            # increase the vertex previous value by 1
            previous_vertex_val += 1
        else: # new object_id, reset the object_id and previous_vertex val
            # reset the object_id and vertex value to 1
            previous_object_id = str(row[0])
            previous_vertex_val = 1
            # set vertex in the fc to 1
            row[1] = previous_vertex_val

        # update the row


0 Kudos