Skip to content

Instantly share code, notes, and snippets.

@maptastik
Last active September 25, 2017 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maptastik/375fd883c6d97d3b1454123cd0d62eda to your computer and use it in GitHub Desktop.
Save maptastik/375fd883c6d97d3b1454123cd0d62eda to your computer and use it in GitHub Desktop.
Generate basic summary statistics for a field in a feature class using ArcPy

Generating summary statistics in ArcGIS is a bit more complicated than it needs to be. The Summary Statistics tool requires that you return your output to some sort of tabular data file. Sometimes you just need to see the number really quickly or see how they change if you filter the data differntly. This is function is the beginnings of a simple tool to generate summary statistics for a given field in a feature class. The function returns a dictionary containing the number of entries, the sum of the entries, and mean/median/standard deviatation value of the entries for the inputed field.

import arcpy
import statistics
def basic_fc_stats(fc, field):
field_vals = []
total = 0
row_count = 0
with arcpy.da.SearchCursor(fc, field) as cursor:
for row in cursor:
total = total + row[0]
row_count+=1
field_vals.append(row[0])
mean = statistics.mean(field_vals)
median = statistics.median(field_vals)
stdev = statistics.stdev(field_vals)
summary = {'sum': total, 'row_count': row_count, 'mean': mean, 'median': median, 'stdev': stdev}
return summary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment