Printing the highest value between a range of values of a field

1980
11
Jump to solution
06-11-2021 07:00 AM
Poncio
by
New Contributor II

I'm completly new with python and wanted to do this little project that would be very useful. What I want to do in the end is having a little window pop up and showing the highest value of a field but only for a specific range of values, like between 1000 and 2000. Here's an example:

Field1
1001
1002
1003
2002
2005
3001
3003
3008

What I need in the end is a list like this (but for now I'd be happy to just print out the right values):

Zone1 = 1003

Zone2 = 2005

Zone3 = 3008

 

With advice and code from another place I managed to write this

 

import arcpy
fc = 'C:\..............\Einzelpunkt.shp'
fields = ['Identifika']
#setting up the max value variables
Zone1=0
Zone2=0

# For each row in the attribute table, use if statements
# to determine which category it is in and if it is bigger
# than the current max value for that category, update the
# max value variable
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
if row[0]<20000 and row[0]>Zone1:
Zone1=row[0]
elif row[0]<30000 and row[0]>Zone2:
Zone2=row[0]

#keep adding elif statements to cover the rest of the Zones
#this will print out the last number used in the piano category
print "Zone1="+str(Zone1)
print "Zone2="+str(Zone2)
break

 

 

From what I can understand the if statements only take into account the first value in the column, wich in the example shape I have is ~31000 wich makes the statements false and not change the variables.

How should I approach this?

Keep in mind I'm a complete beginner 🙂

11 Replies
Poncio
by
New Contributor II

Sorry, like last time, we got a lot of work and I kinda left this little project where it was and forgot about this...

In the meantime I found another solution tho: I use the modelbuilder to make what I was doing manually wich is to select by attribute for the zone and get the max value of the selected. After that I use a simple Python script to take the values and visualize them in a separate window. Now it's just a matter of cleaning up the modelbuilder (would need to make the number inside the select SQL a variable wich most likely requires to make it entirely in Python, I tried to convert the model but it's quite messy and definely out of my current competence). For now I'm happy since the result is usable.

0 Kudos
DanPatterson
MVP Esteemed Contributor

divmod   oft forgotten 

field1 = arcpy.da.TableToNumPyArray(...table to use..., ...field name...)
field1 = np.array([101, 102, 103, 202, 205, 301, 303, 308])  # input array
# --- classify, slice, group and solve
np.divmod(field1, 100)  # divisor is 100 in this case
q, r = np.divmod(field1, 100)  #  return the quotient and remainder
split_idx = np.nonzero(np.diff(q))[0]  #  return the indices where they differ
group_vals = np.array_split(r, split_idx+1)  # split the remainder into groups

#  ---- some results
q
array([1, 1, 1, 2, 2, 3, 3, 3], dtype=int32)  # quotient
r
array([1, 2, 3, 2, 5, 1, 3, 8], dtype=int32)  # remainder
split_idx
array([2, 4], dtype=int64)  # the indices for the group

group_vals  # the remainders in each group
[array([1, 2, 3], dtype=int32),
 array([2, 5], dtype=int32),
 array([1, 3, 8], dtype=int32)]

maxs = [g.max() for g in group_vals]  # get the max per group
maxs
[3, 5, 8]

 

Options abound, but divmod rarely gets show-cased.


... sort of retired...