代码示例统计数据示例(Python 窗口)
以下 Python 窗口脚本演示了如何在即时模式下使用 Statistics 工具。
import arcpy
arcpy.env.workspace = "C:/data/Habitat_Analysis.gdb"
arcpy.Statistics_analysis("futrds", "C:/output/output.gdb/stats", [["Shape_Length", "SUM"]], "NM")统计数据示例 2(独立脚本)
以下独立脚本汇总了主要道路 150 英尺范围内的植被区域。
# 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)统计数据示例 3(独立脚本)
以下独立脚本循环遍历数据集的属性字段并构造 statistics_fields 参数,从而计算各个数值字段的 SUM 统计量。
# 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 示例 4(独立脚本)
以下脚本使用 pandas DataFrame 来访问和显示 Statistics 工具的表格结果。
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)