ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Core.Data Namespace / Table Class / CalculateStatistics Method
Specifies how statistics computations should be carried out against one or more fields in a table or feature class.
Example

In This Topic
    CalculateStatistics Method
    In This Topic
    Performs common statistics functions on one or more fields in this table or feature class. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax

    Parameters

    tableStatisticsDescription
    Specifies how statistics computations should be carried out against one or more fields in a table or feature class.

    Return Value

    A list of TableStatisticsResults that report the results of statistics computation on the input fields. If no GroupBy is specified, the count of the returned list is one. However, if tableStatisticsDescription has at least one GroupBy field specified, the returned list count may be more than one where each instance of TableStatisticsResult corresponds to each unique value under the GroupBy field.
    Exceptions
    ExceptionDescription
    tableStatisticsDescription is null.
    One or more StatisticsDescriptions in tableStatisticsDescription's StatisticsDescriptions property have a Field that does not exist in this Table.
    A geodatabase-related exception has occurred.
    Remarks
    Depending on the capability of the underlying data store, not all configuration options are supported. For example, TableStatisticsDescription.GroupBy is not implemented for shapefiles.
    Example
    Calculating Statistics on a Table
    // Calculate the Sum and Average of the Population_1990 and Population_2000 fields, grouped and ordered by Region
    public void CalculateStatistics(FeatureClass countryFeatureClass)
    {
        using (FeatureClassDefinition featureClassDefinition = countryFeatureClass.GetDefinition())
        {
            // Get fields
            Field regionField = featureClassDefinition.GetFields()
              .First(x => x.Name.Equals("Region"));
            Field pop1990Field = featureClassDefinition.GetFields()
              .First(x => x.Name.Equals("Population_1990"));
            Field pop2000Field = featureClassDefinition.GetFields()
              .First(x => x.Name.Equals("Population_2000"));
    
            // Create StatisticsDescriptions
            StatisticsDescription pop1990StatisticsDescription = new StatisticsDescription(pop1990Field,
                      new List<StatisticsFunction>() { StatisticsFunction.Sum,
                                                StatisticsFunction.Average });
    
            StatisticsDescription pop2000StatisticsDescription = new StatisticsDescription(pop2000Field,
                      new List<StatisticsFunction>() { StatisticsFunction.Sum,
                                                StatisticsFunction.Average });
    
            // Create TableStatisticsDescription
            TableStatisticsDescription tableStatisticsDescription = new TableStatisticsDescription(new List<StatisticsDescription>() {
                  pop1990StatisticsDescription, pop2000StatisticsDescription });
            tableStatisticsDescription.GroupBy = new List<Field>() { regionField };
            tableStatisticsDescription.OrderBy = new List<SortDescription>() { new SortDescription(regionField) };
    
            // Calculate Statistics
            IReadOnlyList<TableStatisticsResult> tableStatisticsResults = countryFeatureClass.CalculateStatistics(tableStatisticsDescription);
    
            foreach (TableStatisticsResult tableStatisticsResult in tableStatisticsResults)
            {
                // Get the Region name
                // If multiple fields had been passed into TableStatisticsDescription.GroupBy, there would be multiple values in TableStatisticsResult.GroupBy
                string regionName = tableStatisticsResult.GroupBy.First().Value.ToString();
    
                // Get the statistics results for the Population_1990 field
                StatisticsResult pop1990Statistics = tableStatisticsResult.StatisticsResults[0];
                double population1990Sum = pop1990Statistics.Sum;
                double population1990Average = pop1990Statistics.Average;
    
                // Get the statistics results for the Population_2000 field
                StatisticsResult pop2000Statistics = tableStatisticsResult.StatisticsResults[1];
                double population2000Sum = pop2000Statistics.Sum;
                double population2000Average = pop2000Statistics.Average;
    
                // Do something with the results here...
            }
        }
    }
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also