[GIS] Determining Min/Max values from feature class using ArcPy

arcgis-10.2arcpycsvexport

I am very rusty/inexperienced in Python.

I would like to create a more compact script that prints min and max values of each numeric field from a feature class. Right now I have the code below repeated for each field that contains numeric values.

How would I code this to identify if a field has numeric values and then print the min and max values to the python shell or even better to a .csv/text file?

import arcpy
data = "C:/Workfolder/Input.gdb/FeatureClass"

listname = []
cursor = arcpy.da.SearchCursor(data, "field1")

for row in cursor:
    listname.append(int(row[0]))

print 'Field1_min, {0}'.format(min(listname))
print 'Field2_max, {0}'.format(max(listname))

Best Answer

I think one of the efficient options would be using where and sql_clause arguments of arcpy.da, for example:

field_to_find_min_max = "MyFieldName"
min_value = arcpy.da.SearchCursor(data, field_to_find_min_max, "{} IS NOT NULL".format(field_to_find_min_max), sql_clause = (None, "ORDER BY {} ASC".format(field_to_find_min_max))).next()[0]
max_value = arcpy.da.SearchCursor(data, field_to_find_min_max, "{} IS NOT NULL".format(field_to_find_min_max), sql_clause = (None, "ORDER BY {} DESC".format(field_to_find_min_max))).next()[0]

This also makes sure the only non-NULL values are processed (where clause).

Related Question