Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a point shapefile that have two attributes that I want to extract from them. These two fields are "Group" and "Distance"

I want to grab the maximum "distance" from each unique number in the field "group". Max Value From Each Group

I was wondering if any knew of a method of using arcpy searchcursor to find this max value? Writing something like

rows = arcpy.SearchCursor(input)
  Counter = 0
  for row in rows:
    if row.NEAR_DIST > Counter:
      Counter = row.NEAR_DIST

will only give me the max value of the entire dataset as a whole? Any ideas?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Try this out:

import arcpy

shp = r'C:\Data\Test\test.shp'
rows = arcpy.SearchCursor(shp)

#--first, lets get a list of all unique Group numbers
groups = []
for row in rows:
    if row.Group not in groups:
        groups.append(row.Group)
del row

#--now, lets go back through all rows and find the max for each group
#--and store the results in a dictionary
out_dict = {}
for row in rows:
    for g in groups:
        lst = []
        if row.Group == g:
            lst.append(row.Distance)
        out_dict(g) = max(lst)
del row,rows
for group in out_dict:
    print group
share|improve this answer
1  
A better way to do this would be to sort your table using the "Group" field when you make your search cursor, then you can iterate through your rows and calculate the maximum values as the value in that field changes; that way you only iterate through your rows once. –  Jason May 22 '12 at 14:57
    
Thanks jason that is a good suggestion and in the end I applied something similar using a dictionary to define the max value of each group! –  BJEBN May 22 '12 at 15:33
    
That's great, glad you got it figured out! –  Jason May 22 '12 at 15:45
add comment

I just don't know about this answer. I tried to get it to work but it won't return the correct max(lst) value. In any case, there is at least one syntax error (should be out_dict[g], not out_dict(g)). It did, however, expose me to dictionaries... and that's a good thing.

I think using the following code would work just fine:

import arcpy

indata = r"C:\indata.shp"
g = ""

for row in arcpy.SearchCursor(indata,"","","", "GROUP A; DISTANCE D"):
if g != row.GROUP:
    g = row.GROUP
    print row.GROUP
    print row.DISTANCE

In any case, this works for me...

share|improve this answer
add comment

with SearchCursor:

import arcpy

workspace = "C:\Data\Test\test.shp"
recs = arcpy.SearchCursor(workspace)

ary = []

for rec in recs:
   val = rec.getValue('Distance')
   ary.append(val)

print max(ary)  

or you can try Summary Statistics (Analysis) tool for your needs. for more information check out here..

Summary

Calculates summary statistics for field(s) in a table.

.

Syntax

Statistics_analysis (in_table, out_table, statistics_fields, {case_field})

Example:

import arcpy
from arcpy import env
env.workspace = "C:/data/Habitat_Analysis.gdb"
arcpy.Statistics_analysis("futrds", "C:/output/output.gdb/stats", [["Shape_Length", 
"MAX"]], "NM")

i hope it helps you...

share|improve this answer
    
Thanks... thats another way to get the max value of the entire dataset but I want the max value of each group (i.e. in the example above 3 max values). –  BJEBN May 22 '12 at 13:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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