0

I want to write Python code that removes duplicate features from a table based on their attribute value. Within the code, a loop will iterate through multiple attributes to check if the value is Null. If the attribute is not null, it is assigned to a "keep list." If the attribute is null, it is assigned to a "delete list."

How can I compare multiple values (Null and not Null) within the same attribute?

(row[0]a and row[0]b)

import arcpy                

fc = "F:/path/"               
drTable = "F:/path/"          
workspace = "F:/path/"        

def main():                                                       
    arcpy.MakeFeatureLayer_management(fc, "fc_lyr")               
    arcpy.JoinField_management("fc_lyr", "OBJECTID", drTable, "OBJECTID", "KEEP_COMMON") 

    deleteList = []          
    keepList = []

    with arcpy.da.SearchCursor("fc_lyr", ['OBJECTID']) as cursor:
        for row in cursor:
            arcpy.SelectByLocation_management("fc_lyr", "ARE IDENTICAL TO", "fc_lyr", 
            "NEW_SELECTION") 
            if arcpy.GetCount_management("fc_lyr") >= 2:             
                with arcpy.da.SearchCursor("fc_lyr", ['attr1','attr2','attr3',]) as cursor:    
                    for row in cursor:                              
                        if row[0] is None and row[0] is None:       
                                                        #???
                        elif row[0] is None and row[0] is not None:  
                            deleteList.append(row[0])                
                        elif row[0] is not None and row[0] is None:  
                            deleteList.append(row[0])                
                        elif row[0] is not None and row[0] is not None:  
                                                         #???

3
  • Can you add a screenshot showing some sample data? Are you saying that you can have one text field with the value None somethingelse?
    – BERA
    Aug 12, 2021 at 5:38
  • 1
    arcpy.management.DeleteIdentical("fc_lyr", ['attr1','attr2','attr3'])
    – FelixIP
    Aug 12, 2021 at 9:35
  • @FelixIP, Initially I thought the DeleteIdentical tool would be the obvious 1-liner but using as-is would also delete rows that are all the same but not all Null!
    – Hornbydd
    Aug 12, 2021 at 9:58

1 Answer 1

1

If you want to use a cursor, try this:

...    
where = "{0} IS NULL OR {1} IS NULL OR {3} IS NULL".format(arcpy.AddFieldDelimiters(datasource="fc_lyr", field='attr1'), 
                                            arcpy.AddFieldDelimiters("fc_lyr", 'attr2'),
                                            arcpy.AddFieldDelimiters("fc_lyr", 'attr3'))

with arcpy.da.UpdateCursor("fc_lyr", ['attr1','attr2','attr3'], where) as cursor:    
    for row in cursor:                              
        cursor.deleteRow()

But I would guess this is faster:

...
arcpy.SelectLayerByAttribute_management(in_layer_or_view="fclyr", where_clause=where)
arcpy.DeleteRows_management(in_rows="fc_lyr")
                                        

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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