identify change in row sequence

580
3
Jump to solution
08-22-2019 09:00 AM
CliveCartwright
Occasional Contributor

I'm trying to find a way of adding <Null> to a record when there a change in the data sequence on another field attribute...?

I've been trying to adapt this code to achieve this, but I'm struggling. 

import arcpy

from arcpy import da

vertices = "dataset"

#set firstRow variable to true
firstRow = True

with arcpy.da.UpdateCursor(vertices, ["Shape", "Line_ID", "angle"]) as cursor:

for row in cursor:
 name2 = row[1]

#check if its the first row
 if firstRow == True:
 name1 = name2
 #set firstrow to false
 firstRow = False
 continue
 row[2] = str(name1 - name2)
 name1 = name2
 cursor.updateRow(row)

The result should be something like this: 

I'm rather new to UpdateCurser, so, generally don't know how it works.  

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Does this work for you?

import arcpy
from arcpy import da

vertices = "dataset"

#set firstRow variable to true
firstRow = True

with arcpy.da.UpdateCursor(
    vertices,
    ["Line_ID", "angle"]
    sql_clause=(None, "ORDER BY LINE_ID, OBJECTID")
) as cursor:
    lid_prev, ang_prev = next(cursor)
    cursor.updateRow([lid_prev, None])
    for lid, ang in cursor:
        if lid != lid_prev:
            cursor.updateRow([lid, None])
        lid_prev = lid

View solution in original post

3 Replies
JoshuaBixby
MVP Esteemed Contributor

Does this work for you?

import arcpy
from arcpy import da

vertices = "dataset"

#set firstRow variable to true
firstRow = True

with arcpy.da.UpdateCursor(
    vertices,
    ["Line_ID", "angle"]
    sql_clause=(None, "ORDER BY LINE_ID, OBJECTID")
) as cursor:
    lid_prev, ang_prev = next(cursor)
    cursor.updateRow([lid_prev, None])
    for lid, ang in cursor:
        if lid != lid_prev:
            cursor.updateRow([lid, None])
        lid_prev = lid
CliveCartwright
Occasional Contributor

Much appreciated Joshua. Thank You. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If my suggestion worked, please mark it correct to close the question.

0 Kudos