I am using Arcgis Desktop 10.2. Using python script i am trying to resolve the following :

enter image description here

I have 2 columns of data, one containing alphanumeric values, the other some strings like "-1", "-2", "-3" etc.

"-1" means that the data in the first column in no longer needed. The data set is large, millions of rows/ I am supposed to find duplicate string in the first columns. For every match , if all of the rows are marked with "-1" then that value will be removed, or marked by a commentary in another column. If for the match one value(or more) are not "-1" then that value will ignored

Looking at the picture:

  • "string_1" is found 5 times. Because the last instance is not marked "-1" all the 5 "string_1"'s will remain in the table.

  • "string_2" is found 4 time. The last values is not marked with "-1", but with "-4", so all the 4 values remain in the table

    • "string_3" is found 5 times, and all of this values are marked with "-1". For each of this values a commentary must be made in another column, so that in the future i can remove them form the table.

How can i mach the values in the first column (i need the script to find for itself the duplicate values, because i can't tell it what it is supposed to find, there are millions of values) and then find the duplicates that ALL have "-1" attribute?


I have written a short snippet code with simple syntax, so it will be easy to understand for a beginner. The flow is that you need to get the unique values in the Strings field and then find out whether there are more than one value in the Integers field for this String.

enter image description here

If yes - then keep, if no (i.e., the only value was -1) - then delete the row.

import arcpy

in_table = r"C:\Montgomery.gdb\Landbase\Blocks"
fields = ["StringField","IntegerField"] #replace with own
curStrings = arcpy.da.SearchCursor(in_table,fields,"StringField is not null")

stringsKeysList = []
numbersValuesList = []
tableDict = {}

for row in curStrings: 
    print row[0],row[1]
del curStrings
uniqueStringsList = list(set(stringsKeysList)) #only unique values from Strings field

for uniqueString in uniqueStringsList:
    emptyTempValues = []
    curValues = arcpy.da.SearchCursor(in_table,fields,"""StringField = '{0}'""".format(str(uniqueString)))
    for row in curValues:
        print row[1]
        uniqueEmptyTempValues = list(set(emptyTempValues))
    tableDict[uniqueString] = uniqueEmptyTempValues

print tableDict #a dictionary with string:values
#{u'String_2': [-4, -2], 
#u'String_3': [None, -3], 
#u'String_56': [-1], 
#u'String_1': [-1], 
#u'String78': [-1]}

rowsToDelete = [k for k, v in tableDict.iteritems() if v == [-1]] #find which rows to delete
print "list: ", rowsToDelete
stringRows = ","
updRows = stringRows.join(rowsToDelete)
updRows = updRows.replace(",","','")
print updRows #update the string to be used with proper syntax in where clause

where_clause = """StringField in ('{0}')""".format(updRows)
upd_cur = arcpy.da.UpdateCursor(in_table,fields,where_clause)
for row in upd_cur:
    print row[0],row[1]
#    deleteRow(row) uncomment when things work properly
  • i am going to give it a try. I see da.cursor. Is it necessary to delete the cursor? – Bogdan Mircea Stanciu Feb 3 '15 at 13:49
  • could you please point how , in the case of the rows to be deleted, instead of deleting the row the script would write something like "deleteMe" in a separate column? I've used the new update.cursor but methods are new to me, and it is difficult to decipher the text – Bogdan Mircea Stanciu Feb 3 '15 at 15:56
  • It is not necessary to delete the cursors, but it is always good to do that imo. You could just do a selection on the feature class with the where_clause you receive and do the field calculation (either update cursor and then update row or use Calculate Field GP tool - it will respect the selection). More on cursors: resources.arcgis.com/en/help/main/10.2/index.html#//… – Alex Tereshenkov Feb 3 '15 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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