3

I have a feature class in a file geodatabase using ArcGIS 10.1. I want to create a model that selects the three features with the highest values in one field (Average Price). Basically I want my selection to include the three features with the highest average prices.

How do I accomplish this?

5

3 Answers 3

2

I would just do something like this:

field = "AveragePrice"

values = [row[0] for row in arcpy.da.SearchCursor(fc, field)]
top_three = sorted(values)[-3:]

query = "\"{0}\" in {1}".format(field, tuple(top_three))

arcpy.MakeFeatureLayer_management(fc, "top_3_layer", query)
1

One way to solve this would be to use a sql clause in a arcpy.da.SearchCursor to sort your values in descending order. Then you would store the third value of the sorted column. The trick is that you only want to get the third highest so you can utilize a counter variable. Without testing the code something like this could get the job done:

searchFields = ‘AveragePrice’
query = ‘AveragePrice’
sqlClause = (None, ‘ORDER BY ‘ + query +  ‘ DESC’)
ct = 0
with arcpy.da.SearchCursor(fc, field_names = searchFields, sql_clause = sqlClause) as searchCursor:
    for row in searchCursor:
        if ct == 3:
            break
        elif ct == 2:
            thirdHighestPrice = row[0]
        else:
            ct += 1
            continue

Then feed that value of thirdHighestPrice into a arcpy.SelectLayerByAttribute_management statement to select all values that are greater than or equal to the variable thirdHighestPrice.

3
  • 1
    I was hoping to be able to use model builder though...
    – ge0m3try
    Commented Mar 11, 2014 at 20:14
  • Python scripts in the form of custom tools can be used in Model Builder. See ERSI documentation here link.
    – GeoSharp
    Commented Mar 11, 2014 at 21:14
  • What does the None in the SQL do in this instance?
    – ianbroad
    Commented Mar 25, 2014 at 17:11
0

Its not going to be a eloquent solution, but using "Summary Statistics" tool, you could get the MAX value of an attribute. Then do a join using that MAX value to select from your FC that record, remove that record from your original FC and put it aside (Switch Selection). Repeat three times and merge the outputs.

4
  • Hmm that sounds interesting, but is there a way to get have the selection occur within the existing FC?
    – ge0m3try
    Commented Mar 11, 2014 at 20:02
  • You would do the join, and retain only matching values, or have it return all values and then select where FC Value = Summary Stat Max Value.
    – BretW
    Commented Mar 11, 2014 at 20:30
  • Ok, this makes sense and all, but I am getting lost where you say do a join using the max value. How do I do this?
    – ge0m3try
    Commented Mar 11, 2014 at 20:36
  • If you join from Stat Summary to your FC, join based on MAX Value to Attribute value - the only match in your FC will be that record with the max value.
    – BretW
    Commented Mar 12, 2014 at 13:06

Your Answer

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

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