AnsweredAssumed Answered

How to delete duplicate rows where multiple values are duplicated?

Question asked by geoffreywestgis on Aug 12, 2015
Latest reply on Aug 14, 2015 by bixb0012

I have a feature class where there are multiple values duplicated in a row, i.e. SRNumber and RESOLUTION_CODE. I would like to delete all rows where my SRNumber and RESOLUTION_CODE are found in multiple rows. I have got a good start with this script, everything works aside from actually deleting the rows. How can I fix this so that my duplicate rows are deleted?

So if SRNumber = 1-23456 and Resolution_Code = 'A' and appears twice, I would like to delete one.

I have taken this example for GIS Stackexchange, but the del rows does not work correctly.

 

import arcpy
... 
... in_table = r"ServiceRequest.DBO.SO_Dead_Animal"
... fields = ["NumberCYLA","RESOLUTION_CODE"] #replace with own
... curStrings = arcpy.da.SearchCursor(in_table,fields,"NumberCYLA is not null")
... 
... stringsKeysList = []
... numbersValuesList = []
... tableDict = {}
... 
... for row in curStrings: 
...     print row[0],row[1]
...     stringsKeysList.append(row[0])
... del curStrings
... uniqueStringsList = list(set(stringsKeysList)) #only unique values from Strings field
... 
... for uniqueString in uniqueStringsList:
...     emptyTempValues = []
...     curValues = arcpy.da.SearchCursor(in_table,fields,"""NumberCYLA = '{0}'""".format(str(uniqueString)))
...     for row in curValues:
...         print row[1]
...         emptyTempValues.append(row[1])
...         uniqueEmptyTempValues = list(set(emptyTempValues))
...     tableDict[uniqueString] = uniqueEmptyTempValues
... 
... print tableDict
... 
... 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 = """NumberCYLA in ('{0}')""".format(updRows)
... upd_cur = arcpy.da.UpdateCursor(in_table,fields,where_clause)
... for row in upd_cur:
...     print row[0],row[1]
...     del rowsToDelete

Outcomes