Solved! Go to Solution.
try: #update table with difference value with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BIRTH", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BIRTH = " + str(list2[0]) + " OR FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BIRTH = " + str(list2[1])) as cursor: for row in cursor: row[2] = DIFFERENCE cursor.updateRow(row) del row, cursor except: pass
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" table = "data" # create empyt list to append ID field list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["id"]) as cursor: for row in cursor: list.append(row[0]) del row, cursor #remove duplicate IDs list = dict.fromkeys(list) list = list.keys() #iterate through list for id in list: list2 = [] with arcpy.da.SearchCursor(table, ["id", "age", "difference"], "id = " + str(id)) as cursor: for row in cursor: #append age to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference difference = list2[-1] - list2[-2] except: pass del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["id", "age", "difference"], "age = " + str(list2[-2]) + " AND id = " + str(id)) as cursor: for row in cursor: row[2] = difference cursor.updateRow(row) except: pass del row, cursor
Hi Clinton,
Here is an example on how you can do this:import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" table = "data" # create empyt list to append ID field list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["id"]) as cursor: for row in cursor: list.append(row[0]) del row, cursor #remove duplicate IDs list = dict.fromkeys(list) list = list.keys() #iterate through list for id in list: list2 = [] with arcpy.da.SearchCursor(table, ["id", "age", "difference"], "id = " + str(id)) as cursor: for row in cursor: #append age to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference difference = list2[-1] - list2[-2] except: pass del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["id", "age", "difference"], "age = " + str(list2[-2]) + " AND id = " + str(id)) as cursor: for row in cursor: row[2] = difference cursor.updateRow(row) except: pass del row, cursor
Just so I understand you correctly. In your example:
ID AGE NEWID DIFFERENCE
2259 1955 22591955 n/a
2259 1956 22591956 1
2259 1986 22591986 n/a
2259 1989 22591989 n/a
The 'Difference' value is not being calculated between 1986 and 1989 because the age from 1955 and 1956 is larger when compared to todays date. Is that correct?
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" table = "data" # create empyt list to append ID field list = [] # iterate through table and append ID to list with arcpy.da.SearchCursor(table, ["id"]) as cursor: for row in cursor: list.append(row[0]) del row, cursor #remove duplicate IDs list = dict.fromkeys(list) list = list.keys() #iterate through list for id in list: list2 = [] with arcpy.da.SearchCursor(table, ["id", "age", "difference"], "id = " + str(id)) as cursor: for row in cursor: #append age to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference difference = list2[1] - list2[0] except: difference = '' del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["id", "age", "difference"], "age = " + str(list2[0]) + " AND id = " + str(id)) as cursor: for row in cursor: row[2] = difference cursor.updateRow(row) except: pass del row, cursor
Runtime error Traceback (most recent call last): File "<string>", line 9, in <module> RuntimeError: An invalid SQL statement was used. [SELECT id, age, OBJECTID FROM WAYNE WHERE id = <built-in function id>]
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" table = "table" list2 = [] with arcpy.da.SearchCursor(table, ["id", "age"], "id = " + str(id)) as cursor: for row in cursor: #append age to new list list2.append(row[1]) #sort list list2.sort() try: #calculate the difference difference = list2[-1] - list2[-2] except: pass del row, cursor try: #update table with difference value with arcpy.da.UpdateCursor(table, ["id", "age", "difference"], "age = " + str(list2[-2]) + " AND id = " + str(id)) as cursor: for row in cursor: row[2] = difference cursor.updateRow(row) except: pass del row, cursor