I want to update the 'Start_End' field in the following shapefile attribute table:
The attribute table consists of 6,134 records. For every unique value in the 'Id' field, I'd like to update each 'Start_End' record with a string, either 'Start' or 'End', depending on the value of 'OID_copy'. For example, for every unique value of 'Id' with the smallest value in the 'OID_copy' field, the 'Start_End' field should be populated with the word 'Start'. Conversely, for every unique value of 'Id' with the largest value in the 'OID_copy' field, the 'Start_End' field should be populated with the word 'End'.
Note: both the 'Id' and the 'OID_copy' fields are integer types, and the 'Start_End' field is string.
My initial thought was that a dictionary and an update cursor would do the trick, but so far I have been unsuccessful at updating the attribute table. My code doesn't throw an error, furthermore, the dictionary seems to have been created correctly. It looks like {1: [0, 1], 2: [2, 3], 3: [4, 5], ...}. However, the 'Start_End' field doesn't get updated.
# Start_End_UpdateAttribute.py
# purpose: For each unique 'Id' value, populate the 'Start_End' field in the valley centerline points shapefile attribute table
# with "start" and "end" using the value of the 'OID_copy' field as criteria
import arcpy
import traceback
arcpy.env.workspace = r'D:/GIS_data/Regional/Washington/ValleyBottom_UC_SF/ValleyBottom_UC_SF_centerlines/Wenatchee'
fc = r'D:/GIS_data/Regional/Washington/ValleyBottom_UC_SF/ValleyBottom_UC_SF_centerlines/Wenatchee' \
r'/VC_ChiwawaRiver_split_verts_Copy.shp'
fields = ['Id', 'OID_copy', 'Start_End']
idDict = {}
cursor = arcpy.da.SearchCursor(fc, fields)
try:
for row in cursor:
id = row[0]
oid = row[1]
stend = row[2]
idDict.setdefault(id, []).append(oid)
except:
print 'An error occurred'
traceback.print_exc()
del cursor
print idDict # output is {key: [value, value], key: [value, value], etc}
cursor2 = arcpy.da.UpdateCursor(fc, fields)
try:
for row in cursor2:
for k, v in idDict.items():
if v == min(v):
row[2] = "Start"
cursor2.updateRow(row)
else:
row[2] = "End"
print 'Updated \'Start_End\' field'
except:
print 'An error occurred - part 2'
traceback.print_exc()
del cursor2
How should the second block of my code be revised to produce an attribute table that looks like this?:
id
? – Aaron♦ Feb 28 '20 at 22:34