Select to view content in your preferred language

To select a record based on the highest value  of a field?

5464
25
Jump to solution
07-09-2013 06:17 AM
ionarawilson1
Deactivated User
Is there a way to select a record based on the highest value  of a field? And also is there a way to check if a record is selected based on the highest value before I update a record? Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Alum
You could use a cursor with a sort on it:

infc = my input feature class
myField = field I am after the max value from
OID = ObjectID field of my FC.


maxValue = arcpy.SearchCursor(infc, "", "", "", myField+ " D").next().getValue(OID)  


change the myField field to the one you want the max value from and grab the "OID" of that record as maxValue variable. 

this would give you the values, could then use that to select by.

R_


NICE!!! I will def use this one, Rhett.

View solution in original post

0 Kudos
25 Replies
JamesCrandall
MVP Alum
Is there a way to select a record based on the highest value  of a field? And also is there a way to check if a record is selected based on the highest value before I update a record? Thanks


I don't have a specific example to show, but I can think of a few ways to go:

1. Summary Statistics usage.  This would require you to output a table to some location, then you could grab the max value for the field you are summarizing on.  Probably not best for an in-memory workflow though.  http://resources.arcgis.com/en/help/main/10.1/index.html#//00080000001z000000

2. You could convert your fc/table to a numpy array and then derive the the max value from the array.  This may be your most direct approach and can be done without writing anything to disk.  Look at the code sample in the link as it shows a very simplified way to get your statistic (you could just set a variable to your max value then use it later in your updateCursor).

http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000015000000

3. I suppose you could also just create a basic list of values from your field you want to summarize the max value for and set this to a variable.

I'd probably opt for the #2nd option myself.
j
0 Kudos
RhettZufelt
MVP Notable Contributor
You could use a cursor with a sort on it:

infc = my input feature class
myField = field I am after the max value from
OID = ObjectID field of my FC.


maxValue = arcpy.SearchCursor(infc, "", "", "", myField+ " D").next().getValue(OID) 



change the myField field to the one you want the max value from and grab the "OID" of that record as maxValue variable. 

this would give you the values, could then use that to select by.

R_
0 Kudos
JamesCrandall
MVP Alum
You could use a cursor with a sort on it:

infc = my input feature class
myField = field I am after the max value from
OID = ObjectID field of my FC.


maxValue = arcpy.SearchCursor(infc, "", "", "", myField+ " D").next().getValue(OID)  


change the myField field to the one you want the max value from and grab the "OID" of that record as maxValue variable. 

this would give you the values, could then use that to select by.

R_


NICE!!! I will def use this one, Rhett.
0 Kudos
RhettZufelt
MVP Notable Contributor
Basically, it is just sorting the table decending by myField, then grabbing the "first" row.

Change the " D" to " A" and will sort ascending and would grab the minimum value instead.

R_
0 Kudos
ionarawilson1
Deactivated User
Hi Rhett, so if I want to update something after the search cursor, what do I do. I tried to update a field but of course it did not work, so in this example, what should I change? Thank you so much !!!

    maxValue = arcpy.SearchCursor("Stewardship", "", "", "", "OBJECTID  A").next().getValue("OBJECTID")
    with maxValue as rows:
        # row comes back as a tuple in the order specified here, so Office is row[0], Forester is row[1]
        yearonly = DateStart[5:9]

        for row in rows:
            row[0] = yearonly


            rows.updateRow(row)

0 Kudos
ionarawilson1
Deactivated User
Rhett, how can I run any process after I create the search cursor, because right now the cursor is pointing to an integer object (OBJECTID), but how can I select this value with the highest OBJECTID and use in a process? Right now what I have below is not going to work because it is going to look at the records and not only the one with the highest OBJECTID

Thank you!!!!
  maxValue = arcpy.SearchCursor("Stewardship", "", "", "", "OBJECTID  A")
    for srow in maxValue:
     result = int(arcpy.GetCount_management("Stewardship").getOutput(0))
     arcpy.AddMessage(result)
0 Kudos
ionarawilson1
Deactivated User
I think I am getting there but still getting an error for the first line. Do  you see anything wrong on the first line?  Thanks!!!!
I am getting this error:


Error Info:
     <type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function.


 maxValue = arcpy.SearchCursor("Stewardship", "", "", "", "OBJECTID  A").next().getValue("OID")
    arcpy.SelectLayerByAttribute_management("Counties",  "NEW_SELECTION", maxValue)
    result = int(arcpy.GetCount_management("Stewardship").getOutput(0))
    arcpy.AddMessage(result)
0 Kudos
RhettZufelt
MVP Notable Contributor
I think I am getting there but still getting an error for the first line. Do  you see anything wrong on the first line?  Thanks!!!!
I am getting this error:


Error Info:
     <type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function.


 maxValue = arcpy.SearchCursor("Stewardship", "", "", "", "OBJECTID  A").next().getValue("OID")
    arcpy.SelectLayerByAttribute_management("Counties",  "NEW_SELECTION", maxValue)
    result = int(arcpy.GetCount_management("Stewardship").getOutput(0))
    arcpy.AddMessage(result)


Got a fire going in a few minutes, so I'll have to try to get back to previous posts later, but it seems you are on the right track.  However, will need to make a feature layer before selection as selectbyatt can't utilize a FC directly.

As far as the error, probably your OID field name, it can't be both.  you are sorting on OBJECTID, but trying to extract a value from OID.

R_

R_
0 Kudos
RhettZufelt
MVP Notable Contributor


    rows = arcpy.UpdateCursor("Stewardship", "", "", "", "OBJECTID  A")
       row = rows.next()
       row.yearonly = DateStart[5:9]
       rows.updateRow(row)

       


another quick thought before I run, UpdateCursor supports the sort also, might try something like above and see if it works correctly.

This is how I define row(s) values without having to actually iterate through the cursor.  Of course, you need to have a field in Stewardship FC named "yearonly".

Should update the field "yearonly" to the value in stored in the variable DateStart[5:9].

R_

Of course, I noticed that the " D" was changed to " A", so this should actually grab the minimum value, not the max.
0 Kudos