For Loop Attribute Field Python

736
2
Jump to solution
01-20-2023 04:49 AM
Labels (2)
TimothyODoherty
New Contributor II

Hi,

 

I have an attribute table with over 760,00 polylines (River Segments). I want to group these river segments into 380 individual features (Rivers). I need a python script / ModelBuilder method that can for loop over a attribute field, that every time the value reverts back to 0 it creates a feature or inputs an incremented value into another field (RiverID). This can be seen in the attached photos in the field "segmentInd'; as this field is the number of measurements from the river source in an ascending order downstream.

There are no other fields that are unique to allow me to select by them. 

 

I am new to python and do not know how to for loop over an attribute table.

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

 

Copy/paste this script into the Python Window, change the name of the layer, execute

river_id = 0
with arcpy.da.UpdateCursor("NameOfTheLayer", ["segmentInd", "RiverID"]) as cursor:
    for row in cursor:
        if row[0] == 0:
            river_id += 1
        cursor.updateRow([row[0], river_id])

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

 

Copy/paste this script into the Python Window, change the name of the layer, execute

river_id = 0
with arcpy.da.UpdateCursor("NameOfTheLayer", ["segmentInd", "RiverID"]) as cursor:
    for row in cursor:
        if row[0] == 0:
            river_id += 1
        cursor.updateRow([row[0], river_id])

Have a great day!
Johannes
TimothyODoherty
New Contributor II

Johannes Thank you so much. This worked like a charm!

0 Kudos