Min and Max values in an attribute table

11512
3
01-27-2012 07:40 AM
JeremyTibbetts1
New Contributor
This seems like a pretty straight forward question, but I can't seem to find the answer online so I am hoping someone can help me.

1) I have a field in an attribute table, lets call it Index_values and I would like to find the range of values within that field.
My plan was to use the min() and max() function, giving me the lowest and highest value.

2) Then I would like to break this range up into 5 equal categories.
How I was going to do this was max - min, then divide that by 5

example max = 12, min = 2

12 - 2/5 = 2 

Then I would add 2, to the min value and go up from there...

Therefore, that categories would be:
2-4
4-6
6-8
8-10
10-12


However, my problem is, I cannot get the min/max function to look through the Index_value field within that table.
Is there something I am missing, or an easier way to find the range and divide that range into categories?

Thanks!

Jeremy
Tags (2)
0 Kudos
3 Replies
GerryGabrisch
Occasional Contributor III
Use a search cursor and append all the values from that attribute to a new Python list.  Sort the list.  The first item in the list is the min value and the last is the last value.
theitems = []
rows = arcpy.SearchCursor(inlayer)
for row in rows:
    theitems.append(row.getValue(somefieldname))
del rows
    
theitems.sort()
min = theitems[0]
max = theitems[-1]
ChrisSnyder
Regular Contributor III
Another way, which is faster and involves fewer lines of code... But for which you can't use derive other usefull stats such as mean or StdDev...

myField = "MY_FIELD"
minValue = arcpy.SearchCursor(inlayer, "", "", "", myField + " A").next().getValue(myField) #Get 1st row in ascending cursor sort
maxValue = arcpy.SearchCursor(inlayer, "", "", "", myField + " D").next().getValue(myField) #Get 1st row in descending cursor sort
0 Kudos
curtvprice
MVP Esteemed Contributor
Dan Patterson has posted some Python code in arcscripts (not arcpy, but the translation is not difficult) here:

http://arcscripts.esri.com/details.asp?dbid=14173

If you want to take the approach of creating a python list, there are some great methods in python:

http://adorio-research.org/wordpress/?p=125

Another tack would be to build a little script or model that uses Get Count, Sort and Calculate Field to just do an integer division to determine the class number. This would be simple and fast but wouldn't be very sophisticated for distributions that don't break into quantiles so easily.
0 Kudos