The summary statistics command can be used in arcpy to calculate statistics - this tool runs against a single field, eg:
SUM—Adds the total value for the specified field.
Is there an out-of-the-box method to calculate statistics across multiple fields?
eg I'd like to calculate the MIN, MAX, MEAN and STDEV of Field1, Field2 and Field3, combined:
OID Field1 Field2 Field3 Field4
1 23 24 25 26
2 12 13 14 15
3 17 18 19 20
One approach is to read through the table with a cursor, and calculate these values manually in a script. Is there a better/faster way?
Edit: I need this to run on only the selected records, and these will change frequently.
Answer
Your best bet for this approach would definitely be a cursor. You can create a python tool that takes 2 inputs:
- Feature class/table in question
- multi-value parameter for all the fields you are interested in
The code to grab all this information is pretty straightforward:
import math, itertools, arcpy
FC = arcpy.GetParameterAsText(0)
fields = arcpy.GetParameterAsText(1).split(";")
#Flatten a list of lists
def flatten(list_lists):
return list(itertools.chain.from_iterable(list_lists))
#Whatever standard dev func is applicable
def std(x):
pass
#read values from fields and flatten
vals = flatten([r for r in row] for row in arcpy.da.SearchCursor(FC, fields))
arcpy.AddMessage("\nThe MIN is: {}".format(min(vals)))
arcpy.AddMessage("The MAX is: {}".format((max(vals))))
arcpy.AddMessage("The MEAN is: {}\n".format(sum(vals) / len(vals)))
arcpy.AddMessage("The STD is: {}".format(std(vals)))
If you want to dump the statistics to a table with a timestamp, that can be easily accomplished.
No comments:
Post a Comment