4

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?

3

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
| 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
3

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...

| 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
3

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...

| improve this answer | |
0

Here is another variation using the collections module in Python to create a dictionary in order to solve the problem.

import arcpy, os, collections

fc = r'C:\path\to\your\fc'

zipped = []
with arcpy.da.SearchCursor(fc, ["Group1", "Distance"]) as cursor:
    for row in cursor:
        zipped.append(zip((row[0], row[1])))

d = collections.defaultdict(list)

for k, v in zipped:
    d[k[0]].append(v)

for k, v in d.items():
    print "Group %s has a maximum value of %s" % (k,max(v)[0])
| improve this answer | |
0

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")

by Aragon

This works, in case somebody dismisses it due to BJEBN's comment. It doesn't find the max for the dataset. It finds the max values for unique groups within the dataset.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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