I'm looking for advice about the ArcPy code necessary to find: the highest value, the minor value and the number of single values (not records) of a field in a table.
-
I tried to use the cursor but without resuts– GianniCommented May 3, 2018 at 12:43
-
2Please post the code you used for the cursor. Try adding print statements along the way to debug and let us know at what stage it is giving unexpected results.– SMillerCommented May 3, 2018 at 12:44
Add a comment
|
2 Answers
There are a lot of useful examples posted on Esri's ArcGIS help pages.
For example, here's the code for finding unique values of a field using the search cursor.
import arcpy
fc = 'c:/data/base.gdb/well'
field = 'Diameter'
# Use SearchCursor with list comprehension to return a
# unique set of values in the specified field
values = [row[0] for row in arcpy.da.SearchCursor(fc, field)]
uniqueValues = set(values)
print(uniqueValues)
This was taken from Example 2 on the help pages for search cursor.
The rest are standard python functions -- e.g. len(uniqueValues)
, min(uniqueValues)
, max(uniqueValues)
.
import arcpy
fc = r'C:\database.gdb\feature_class'
value_field = 'Values'
all_values = [i[0] for i in arcpy.da.SearchCursor(fc, value_field)]
print max(all_values) #max
print min(all_values) #min
print len(set(all_values)) #number of unique values