If you want to select the top 20% of the highest values this code should work:
import arcpy
"""Customize the layer name, IDField and valueField"""
lyrName = "sourceLayer"
IDField = "RID"
valueField = "MEAS"
Top20Field = "TOP20PERCENT"
"""Get the current map layers."""
mxd = arcpy.mapping.MapDocument("CURRENT")
"""Find the layer name"""
lyr = arcpy.mapping.ListLayers(mxd, lyrName)[0]
"""Create a dictionary of keys, values and record counts"""
valueDict = {}
with arcpy.da.SearchCursor(lyr, [IDField, valueField, "OID@"]) as searchRows:
for searchRow in searchRows:
keyValue = searchRow[0]
if not keyValue in valueDict:
valueDict[keyValue] = [[(searchRow[1],searchRow[2])], 1]
else:
valueDict[keyValue][0].append((searchRow[1], searchRow[2]))
valueDict[keyValue][1] += 1
print "Dictionary Read"
"""Create an OID List of records that are in the Top 20%"""
OIDList = []
for keyValue in sorted(valueDict.keys()):
valueDict[keyValue][0] = sorted(valueDict[keyValue][0], reverse=True)
top20Percent = int(round(valueDict[keyValue][1] * .2, 0))
for n in range(0, top20Percent):
OIDList.append(valueDict[keyValue][0][1])
"""Write a flag value to the Top20Field to indicate whether or not each record is in the Top 20%"""
with arcpy.da.UpdateCursor(lyr,["OID@", Top20Field]) as updateRows:
for updateRow in updateRows:
if updateRow[0] in OIDList:
updateRow[1] = "Yes"
else:
updateRow[1] = "No"
updateRows.updateRow(updateRow)
If you want the top 20% with the lowest values change line 30 to valueDict[keyValue][0] = sorted(valueDict[keyValue][0])