Select to view content in your preferred language

Subtract two specific values

4831
22
Jump to solution
10-25-2013 06:12 AM
ClintonCooper1
Deactivated User
I have a table that has several records that I have given a sequence number to based on the find identical tool that creates a new field which creates a new sequence number if they are identical.  Some features are unique while others may have up to 12 identical features.  I created a third filed which is a concatenation of the feat_seq field and an age field of which I can sort to get an order of age from oldest to youngest.  I would like to subtract the first two values, the oldest from the second oldest while not preforming this subtraction if there is less than 1 identical feature.  Additionally, if there are more than 3 identical features, it would only subtract the first two and none of the other ones.  Here is a sample of what the data might look like:

id age newid difference in age
2257 72 225772 n/a
2257 62 225762 10
2258 45 225845 0
2259 58 225958 n/a
2259 57 225957 1
2259 27 225927 n/a
2259 24 225924 n/a


I have found some code/thread (http://forums.arcgis.com/threads/28786-Need-Python-Example-sort-rows-subtract-current-from-next-load...)  that will subtract each value starting from the first value all through the end.  I was wondering if it is possible to specify which rows to subtract and which ones not to subtract using python ?

Clinton
Tags (2)
0 Kudos
22 Replies
AllisonBergman
Emerging Contributor
Thank you for your reply.  I tested the following script with the attached data, however the DIFFERENCE field doesn't update.  Can you please help?  Please see the attached data file and code.   Thank you.

import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = r"C:\Users\test\Desktop\TestPythonScripts"

table = "data.dbf"
list = []

# iterate through table and append ID to list
with arcpy.da.SearchCursor(table, ["FEAT_SEQ"]) as cursor:
    for row in cursor:
        list.append(row[0])
        

del row, cursor
#remove duplicate IDs
list = dict.fromkeys(list)
list = list.keys()
list2 = []

for FEAT_SEQ in list:
    with arcpy.da.SearchCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"],"FEAT_SEQ = " +str(FEAT_SEQ)) as cursor:
        for row in cursor:
            #append YEAR_OF_BI to new list
            list2.append(row[1])
    #sort list
    list2.sort()
    try:
        #calculate the difference
        DIFFERENCE = list2[1] - list2[0]
        print list2[1]
        print list2[0]
        print DIFFERENCE
    except:
        DIFFERENCE = ''
    del row, cursor
    try:
            #update table with difference value
            with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) +  "AND YEAR_OF_BI = " + str(list2[0]) + " OR FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[1])) as cursor:
                for row in cursor:
                    row[2] = DIFFERENCE
                    cursor.updateRow(row)
                    print row
            del row, cursor
    except:
            pass

print "Finished Script"
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try the following:

table = "data.dbf"
list = []

# iterate through table and append ID to list
with arcpy.da.SearchCursor(table, ["FEAT_SEQ"]) as cursor:
    for row in cursor:
        list.append(row[0])
        

del row, cursor
#remove duplicate IDs
list = dict.fromkeys(list)
list = list.keys()

for FEAT_SEQ in list:
    list2 = []
    with arcpy.da.SearchCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"],"FEAT_SEQ = " +str(FEAT_SEQ)) as cursor:
        for row in cursor:
            #append YEAR_OF_BI to new list
            list2.append(row[1])
    #sort list
    list2.sort()
    try:
        #calculate the difference
        DIFFERENCE = int(list2[-1]) - int(list2[0])
    except:
        DIFFERENCE = ''
    del row, cursor
    try:
            #update table with difference value
            with arcpy.da.UpdateCursor(table, ["FEAT_SEQ", "YEAR_OF_BI", "DIFFERENCE"], "FEAT_SEQ = " + str(FEAT_SEQ) +  "AND YEAR_OF_BI = " + str(list2[0]) + " OR FEAT_SEQ = " + str(FEAT_SEQ) + "AND YEAR_OF_BI = " + str(list2[1])) as cursor:
                for row in cursor:
                    row[2] = DIFFERENCE
                    cursor.updateRow(row)
            del row, cursor
    except:
            pass

print "Finished Script"


Changes made: 
1.  creating list2 after each iteration through the first list
2.  Specify the first last list value as list2[-1]
3.  Convert the list values to integers
0 Kudos
AllisonBergman
Emerging Contributor
Thank you for taking the time to help, I really appreciate it.  I'm sorry to say that the code didn't update the difference field.  Do you know of a function that pulls out the max and min out of the data search cursor similar to the get value for the old search cursor instead of sorting the data? 

Thanks again,
Allison
0 Kudos