1

I am trying to update a field "TPN" based on another field "PASS_NUM". In my script "TPN" is a newly added number field and thus blank, "PASS_NUM" is also a number field but it contains the wrong numbers. I want to assign values to "TPN" when "PASS_NUM" is no longer equal the the value in the row above it. The feature class table is sorted by "PASS_NUM". See example of attribute table I want to achieve here:

enter image description here

Is there any way to check if the value in the row above is equal to the next value (some sort of onchange event)?

The PASS_NUM field does not increment by 1 each time (i.e. it may skip 7 as in the example) but I want to TPN to only go up one.

Is this possible using the UpdateCursor in arcpy?

closed as off-topic by PolyGeo Oct 21 '17 at 7:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

2

I don't know what your environment is nor how you are querying/updating the table, but conceptually, I'd store a copy of the previous value that I can use to check against in the next loop. Perhaps something like:

oldValue = 0

[code to query current value]

if not currentValue == oldValue:
    TPN = newValue
oldValue = currentValue
1

Try this out. The SQL query may need to be changed depending on your circumstances. I gave this a spin on a few small feature classes and seemed to do the job.

import arcpy

fc = "X:\...\feature_class"

# features may or may not be sorted in attribute table;
# append each value of PASS_NUM to list and sort
print "collecting pass_num values"
pass_numbers = []
for row in arcpy.da.SearchCursor(fc, "PASS_NUM"):
    if row[0] not in pass_numbers:
        pass_numbers.append(row[0])
pass_numbers.sort()
print "found %s values"%str(len(pass_numbers))

# TPN_value starts @ 1 and increases +1 with each pass
print "updating values"
TPN_value = 1
for number in pass_numbers:
    rows = arcpy.da.UpdateCursor(fc, ["TPN"], "PASS_NUM = %s"%str(number))
    for row in rows:
        row[0] = TPN_value
        rows.updateRow(row)
    del rows
    TPN_value += 1

print "finished"

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