3

I am trying to determine the highest 100 values for each district in a shapefile. I have 27 unique districts and 62834 records, but not sure how to proceed. I could "split by attribute" based on the district and select the highest 100 but I would have to repeat this like 30 times for each district.... Is there any easier way or syntax I could use? The value column is number of males in each Small Area Layer in Each district. Using ArcMap 10.3...

  • Sort 62k records in descending order and populate new field using gis.stackexchange.com/questions/200150/… – FelixIP Dec 5 '17 at 17:54
  • Did my answer solve your problem? – BERA Dec 8 '17 at 7:26
  • Sorry, I have been out the office for the past week.. Will try out your solution and get back to you ASAP. Thank you for the assistance! – Maiyuran Kurukkal Dec 12 '17 at 11:06
1

You can do this with a script using the Python window of ArcGIS. Add your shapefile to the map and execute the code. Top values per region will be selected.

import arcpy
from collections import defaultdict

fc = 'Layer_name' #Change to match layer name in table of contents
fields = ['OID@','District','Value_field'] #Change name of District and Value_field to match the field names of your data
number_of_top_values = 100 #Number of top values per district to select

#List all features and sort by district and value field
all_features = sorted([list(i) for i in arcpy.da.SearchCursor(fc, fields)], reverse=True, key = lambda x: (x[1], x[2]))

#Create a dictionary of top values. Regions as keys and Object IDs as values
d = defaultdict(list)
for oid, region, value in all_features:
    if len(d[region]) < number_of_top_values:
        d[region].append(oid)

#Select by object ids
oids_to_select = tuple([item for sublist in list(d.values()) for item in sublist])
sql_clause = '{0} IN {1}'.format(arcpy.AddFieldDelimiters(fc, arcpy.Describe(fc).OIDFieldName), str(oids_to_select))
arcpy.SelectLayerByAttribute_management(in_layer_or_view=fc, where_clause=sql_clause)

Screenshot from ArcGIS Pro but it is the same in ArcGIS Desktop. I have 16 districts in my data:

enter 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.