# arcpy: maximum of a field of a feature class

1762
11
04-26-2019 05:34 AM
by
Occasional Contributor III

Hi

I would like to get the maximum of a field in a feature class.  If no features are selected, I get the right value. But if some features are selected, I am getting the maximum of a field from within these selected features. However, I would like to get maximum of a field from the whole table i.e., from both selected and un-selected features.

how may I do it?

Thank you

11 Replies
MVP Esteemed Contributor
in_fc = 'C:/Your_spaceless_path/CoordGeom.gdb/Polygons'

arr = arcpy.da.TableToNumPyArray(in_fc, 'Shape_Area')  # pick a field

# nan functions account for nulls, cast to the dtype of the field using astype
np.nanmax(arr.astype('float'))

155.0
by
Occasional Contributor III

Thank you, may be I was not clear. Your code gives same answer as mine. Let me try again.

Imagine you have 3 features [0,1,2] in a fc. They have a income field which is an integer [10,12,14] respectively .

case1: I have selected no feature selected, I ask for max of fc , I get 14 >> correct

case2: I have selected features [0,1], i ask for max of fc , I get 12 >> incorrect, I still want 14, because I am asking max of fc, not max of selection of fc.

case3: I have selected features [1,2], i ask for max of fc, I get 14, >> correct

I hope it clarifies a bit?

by
Occasional Contributor III

I mean, I am asking max of income field in fc.

MVP Esteemed Contributor

Just need to ask: have you actually looked at the values of your selected versus non-selected and done the summation manually?  Sounds like one of your fields is 0 or None value.  What is your objective of comparing your selected sum versus the non-selected?  If you know the sum of the income field without any selections, why not write that value to a variable if you need to make comparisons to it?

That should just about do it....
by
Occasional Contributor III

User selects few features, sets their income to max(income in whole of  fc) +1 , so now the max of income of whole fc has changed. User goes to another area in the extent, selects a few polygons, sets their income to max(income in whole of fc) + 1, so the max of income in the whole fc has changed again. This continues till all polygons are finished.

MVP Esteemed Contributor

Just use a Describe object to retrieve the underlying feature class of the feature layer.

featlyr_name = "rangerdistrict" # name of existing feature layer
fld_max = "GIS_ACRES" # field for determining maximum value

max(fld for fld, in arcpy.da.SearchCursor(featlyr_name,fld_max))

fc = arcpy.Describe(featlyr_name).featureclass.catalogPath
max(fld for fld, in arcpy.da.SearchCursor(fc,fld_max))
MVP Esteemed Contributor

My answer gives you the maximum whether you have a selection or not.

If you need that maximum, that is the way to get it without worrying about selections or not.

If you need maximum-ish (ie there is a selection) then work with arcpy

If you want the best of both worlds, they you can work with both approaches

by
Occasional Contributor III

would your solution of accessing a disk file be affected by:

1. lock imposed by arcmap

2. hard-coded path on the file system?

MVP Esteemed Contributor

If you see the requirements for the function, it just requires the path to the featureclass's table.  Locks aren't for reading, only modifying a featureclass.

You could try it and report back