# Min and max in a field attribute with ArcPy?

I have a shapefile with a field attribute called `"Value"`.

I would like to find the `min` and `max` in this field and

• if `max-min > 0.014` return `1` (or `Yes`)
• if `max-min < 0.014` then return `2` (or `No`)

in the field called `"YES_NO"`.

• So all rows will have yes or all will have no?
– BERA
Nov 7, 2018 at 8:07
• It is unclear. Do you want to know if the difference between two successive rows more or less than 0.014, or just max - min values in the Value field more or less than 0.014? Please explain in your question. Nov 7, 2018 at 8:10
• no, can be completely 1 or completely 2 Nov 7, 2018 at 8:11
• in this example the max = 1.1255 and min = 1.0852. max - min = 0.040 so its > 0.014. so the field YES-NO is filled completely by 1 Nov 7, 2018 at 8:15

Use the da.SearchCursor to find min, max and diff. Update field with the UpdateCursor:

``````import arcpy

fc = r'C:\data.gdb\featureclass'
fields = ['Value','YES_NO']

all_rows = [i[0] for i in arcpy.da.SearchCursor(fc,fields[0])]
min_val = min(all_rows)
max_val = max(all_rows)
difference = max_val-min_val

if difference > 0.014:
newval = 1
elif difference <= 0.014:
newval = 2
else:
newval = 3

with arcpy.da.UpdateCursor(fc,fields[1]) as cursor:
for row in cursor:
row[0] = newval
cursor.updateRow(row)
``````