I've got a number of shapefiles with some redundant data. I'm looking for a way to select where a single attribute occurs more than a certain number of times (in this case four) and remove them from the table. As I've searched online, all the results refer to the frequency tool, which is not what I need.

For example, in the image below, FIDs 11,12,13,and 14 are all of interest to me, but 15 and 16 are not. I want to select those and delete them.


I'm using Python because there are hundreds of points in a few dozen shapefiles that I've got, so automation is key.

I'm not looking for somebody to edit my code or write some code for me, I just need an idea of what tools I might use to accomplish this. I don't know how to go about selecting fields when the frequency of an attribute hits a certain frequency.

  • Summary Statistics is better than Frequency... and you don't need and Advanced license. You need to join by attributes to the summary table then select the joined features... FID to NEAR_FID then select JOIN_NAME.COUNT > n where JOIN_NAME is the name of the join and n is the count you want to use to filter... if you can give me a bit more info about how you get to this table from features I can formulate a pythonic answer. – Michael Stimson Apr 26 '16 at 21:45
  • 1
    Are you saying you want to delete or keep only the first 4? Dictionaries are powerful and worth learning. A dictionary whose key is Near_FID and whose value is a list of FID's is one way to approach this. After you've built the dictionary, then you can delete using a list of FID's. learnpythonthehardway.org/book/ex39.html – Kirk Kuykendall Apr 27 '16 at 16:02
  • Michael - Summary Statistics and then a join only gets me a total count. I need to select the 5th, 6th, etc. and delete them. With the join, I just get a count of 7 for each point. – BlakeG Apr 27 '16 at 16:35
  • Kirk - A dictionary might work. I could use NEAR_FID as the key and then select after the nth item in the list and delete them from there. Thanks for the idea! – BlakeG Apr 27 '16 at 16:39

You'll want to use a cursor to figure out the rows that have values that have already occurred four times. Then you can create an SQL and a feature layer or table view with the SQL applied. Finally you can delete rows from the feature layer or table view.

Sample code:

table = r"C:\Users\e1b8\Desktop\E1B8\Workspace\Workspace.gdb\abc123"

#Field to check
field = "VALUE"

#Max number of occurances
maxNum = 4

import arcpy

#dictionary used for counting
di = {}

#list of oids with value counts > maxNum
delOids = []

##iterate table with cursor
with arcpy.da.SearchCursor (table, [field, "OID@"]) as cursor:
    for value, oid in cursor:
        #check if value is in dictionary
        if not value in di:
            #update dictionary
            #key = table value, value = 1
            di [value] = 1

        #if value in dictionary, check if occurrence equals maxNum
        if di [value] == maxNum:
            #add value to list of oids to delete
            delOids += [oid]

        #value in dictionary and occurance is not greater than maxNum
        #add one to dictionary value
        di [value] += 1

#delete cursor
del cursor

#check if OIDs are found
if delOids:

    ##create sql for row deletion
    #get table OID field name
    oidFld = arcpy.Describe (table).OIDFieldName
    #add field delimiters to oid field name
    delimFld = arcpy.AddFieldDelimiters (table, oidFld)

    #create string with comma-seperated oids
    oidStr = ", ".join (map (str, delOids))

    sql = "{0} IN ({1})".format (delimFld, oidStr)

    #create layer or table view
        arcpy.MakeFeatureLayer_management (table, "layer", sql)
    except arcpy.ExecuteError:
        arcpy.MakeTableView_management (table, "layer", sql)

    #delete rows
    arcpy.DeleteRows_management ("layer")

    #delete layer
    arcpy.Delete_management ("layer")

Before and after:

enter image description hereenter image description here

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.