Summary Statistics (Analysis)

Summary

Calculates summary statistics for fields in a table.

Usage

  • The Output Table will consist of fields containing the result of the statistical operation.

  • The following statistical operations are available with this tool: sum, mean, minimum, maximum, range, standard deviation, count, first, last, median, variance, and unique.

  • A field will be created for each statistic type using the following naming convention: SUM_<field>, MEAN_<field>, MIN_<field>, MAX_<field>, RANGE_<field>, STD_<field>, COUNT_<field>, FIRST_<field>, LAST_<field>, MEDIAN_<field>, VARIANCE_<field>, and UNIQUE_<field> (where <field> is the name of the input field for which the statistic is computed). The field name is truncated to 10 characters when the output table is a dBASE table.

  • If a Case field is specified, statistics will be calculated separately for each unique attribute value. The Output Table will contain only one record if no Case field is specified. If one is specified, there will be one record for each Case field value.

  • Null values are excluded from all statistical calculations. For example, the average of 10, 5, and a null value is 7.5 ((10+5)/2).

  • When using layers, only the currently selected features will be used to calculate statistics.

Parameters

LabelExplanationData Type
Input Table

The input table containing the fields that will be used to calculate statistics.

Table View; Raster Layer
Output Table

The output table that will store the calculated statistics.

Table
Statistics Field(s)
(Optional)

Specifies the numeric field or fields containing the attribute values that will be used to calculate the specified statistic. Multiple statistic and field combinations can be specified. Null values are excluded from all statistical calculations.

Text attribute fields can be summarized using first and last statistics. Numeric attribute fields can be summarized using any statistic.

Available statistics types are as follows:

  • Sum—The values for the specified field will be added together.
  • Mean—The average for the specified field will be calculated.
  • Minimum—The smallest value for all records of the specified field will be found.
  • Maximum—The largest value for all records of the specified field will be found.
  • Range—The range of values (maximum minus minimum) for the specified field will be calculated.
  • Standard deviation—The standard deviation of values in the specified field will be calculated.
  • Count—The number of values included in the statistical calculations will be found. Each value will be counted except null values. To determine the number of null values in a field, create a count on the field in question, create a count on a different field that does not contain null values (for example, the OID if present), and subtract the two values.
  • First—The specified field value of the first record in the input will be used.
  • Last—The specified field value of the last record in the input will be used.
  • Median—The median for all records of the specified field will be calculated.
  • Variance—The variance for all records of the specified field will be calculated.
  • Unique—The number of unique values of the specified field will be counted.
Value Table
Case field
(Optional)

The field or fields in the input used to calculate statistics separately for each unique attribute value (or combination of attribute values when multiple fields are specified).

Field

arcpy.analysis.Statistics(in_table, out_table, {statistics_fields}, {case_field})
NameExplanationData Type
in_table

The input table containing the fields that will be used to calculate statistics.

Table View; Raster Layer
out_table

The output table that will store the calculated statistics.

Table
statistics_fields
[[field, {statistic_type}],...]
(Optional)

Specifies the numeric field or fields containing the attribute values that will be used to calculate the specified statistic. Multiple statistic and field combinations can be specified. Null values are excluded from all statistical calculations.

Text attribute fields can be summarized using first and last statistics. Numeric attribute fields can be summarized using any statistic.

Available statistics types are as follows:

  • SUM—The values for the specified field will be added together.
  • MEAN—The average for the specified field will be calculated.
  • MIN—The smallest value for all records of the specified field will be found.
  • MAX—The largest value for all records of the specified field will be found.
  • RANGE—The range of values (maximum minus minimum) for the specified field will be calculated.
  • STD—The standard deviation of values in the specified field will be calculated.
  • COUNT—The number of values included in the statistical calculations will be found. Each value will be counted except null values. To determine the number of null values in a field, create a count on the field in question, create a count on a different field that does not contain null values (for example, the OID if present), and subtract the two values.
  • FIRST—The specified field value of the first record in the input will be used.
  • LAST—The specified field value of thee last record in the input will be used.
  • MEDIAN—The median for all records of the specified field will be calculated.
  • VARIANCE—The variance for all records of the specified field will be calculated.
  • UNIQUE—The number of unique values of the specified field will be counted.
Value Table
case_field
[case_field,...]
(Optional)

The field or fields in the input used to calculate statistics separately for each unique attribute value (or combination of attribute values when multiple fields are specified).

Field

Code sample

Statistics example (Python window)

The following Python window script demonstrates how to use the Statistics tool in immediate mode.

import arcpy
arcpy.env.workspace = "C:/data/Habitat_Analysis.gdb"
arcpy.Statistics_analysis("futrds", "C:/output/output.gdb/stats", [["Shape_Length", "SUM"]], "NM")
Statistics example 2 (stand-alone script)

The following stand-alone script summarizes the vegetation by area within 150 feet of major roads.

# Description: Summarize the vegetation by area within 150 feet of major roads

# Import system modules
import arcpy

# Set environment settings
arcpy.env.workspace = "C:/data"

# Set local variables
inRoads = "majorrds.shp"
outBuffer = "C:/output/output.gdb/buffer_out"
bufferDistance = "250 feet"
inVegetation = "Habitat_Analysis.gdb/vegtype"
outClip = "C:/output/output.gdb/clip_out"
joinField = "HOLLAND95"
joinTable = "c:/data/vegtable.dbf"
joinedField = "HABITAT"
outStatsTable = "C:/output/output.gdb/stats_out"
statsFields = [["Shape_Area", "SUM"]]

# Execute Buffer to get a buffer of major roads
arcpy.Buffer_analysis(inRoads, outBuffer, bufferDistance, dissolve_option="ALL")

# Execute Clip using the buffer output to get a clipped feature class of 
# vegetation
arcpy.Clip_analysis(inVegetation, outBuffer, outClip)

# Execute JoinField to add the vegetation type
arcpy.JoinField_management(outClip, joinField, joinTable, joinField, joinedField)

# Execute Statistics to get the area of each vegetation type within the 
# clipped buffer.
arcpy.Statistics_analysis(outClip, outStatsTable, statsFields, joinedField)
Statistics example 3 (stand-alone script)

The following stand-alone script loops through the attribute fields of a dataset and constructs the statistics_fields parameter so that the SUM statistic is calculated for every numeric field.

# Description: Script that runs the Summary Statistic tool to calculate the
#              Sum statistic for every numeric field based on a unique case 
#              field.

# Import system modules
import arcpy

# Set environment settings
arcpy.env.workspace = "C:/data/f.gdb"

# Set local variables
intable = "intable"
outtable = "sumstats"
casefield = "Name"
stats = []

# Loop through all fields in the Input Table
for field in arcpy.ListFields(intable):

    # Just find the fields that have a numeric type
    if field.type in ("Double", "Integer", "Single", "SmallInteger"):
        # Add the field name and Sum statistic type to the list of fields to 
        # summarize
        stats.append([field.name, "Sum"])
# Correct formatting of stats [["Field1", "Sum"], ["Field2", "Sum"], ...]

# Run the Summary Statistics tool with the stats list
arcpy.Statistics_analysis(intable, outtable, stats, casefield)
Statistics example 4 (stand-alone script)

The following script uses a pandas DataFrame to access and display the tabular results of the Statistics tool.

import arcpy
import pandas
import os

arcpy.env.overwriteOutput = True

in_table = r"d:\data\states.shp"
out_table = r"in_memory\stats_table"
stat_fields = [['POP1990', 'SUM'], ['POP1997', 'SUM']]

stats = arcpy.Statistics_analysis(in_table, out_table, stat_fields,
                                  case_field='SUB_REGION')

# Get a list of field names to display
field_names = [i.name for i in arcpy.ListFields(out_table) if i.type != 'OID']

# Open a cursor to extract results from stats table
cursor = arcpy.da.SearchCursor(out_table, field_names)

# Create a pandas dataframe to display results
df = pandas.DataFrame(data=[row for row in cursor],
                      columns=field_names)

print(df)

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes

Related topics