Count Features in a Feature Class or Table in ArcGIS Pro using ArcPy

Table of Contents

The Video

Introduction

Looking to get the number of features/records in a feature class or table in ArcGIS Pro using ArcPy? 

ArcGIS Pro Definition Query Masterclass with ArcPy & Python

Free on YouTube! Witness the power of Definition Queries in ArcGIS Pro through this comprehensive course that dives managing and utilizing definition queries with spatial data using ArcPy and Python scripting. In this course, we will focus exclusively on Definition Queries, covering their creation, modification, and application through hands-on exercises and real-world scenarios.

User Input

Just the one. The path to the feature class or table.

				
					## the path to the feature class to get the feature count
fc_path = r"C:\path\to\fc_or_table"
				
			

The GetCount Geoprocessing Tool

We use the GetCount() geoprocessing tool from the Data Management Toolbox 

				
					feature_count = arcpy.management.GetCount(
    in_rows = fc_path
)
				
			

Print and Assess

Geoprocessing tools in ArcPy return a Result object that contains the output(s) from the tool. It is good to have knowledge about the Result object and accessing the output generated such as the count of features in a Feature Class from the GetCount tool.

				
					print(feature_count)
#print(int(feature_count)) ## this will fail (TypeError) cannot cast Result to int
print(feature_count[0])
print(type(feature_count[0])) # number is a string
print(int(feature_count[0])) # cast to integer
print(type(feature_count)) # Result object returned from geoproccessing tool.
print(feature_count.outputCount) # GetCount returns one output
print(feature_count.getOutput(0)) # access the output via the Result object
				
			

If you’ve found these blogs helpful and would like to support the project, please consider making a donation. All learning material is provided free of charge to help GIS professionals, students, and developers learn ArcPy, the ArcGIS API for Python, ArcGIS Pro, and ArcGIS Online. Donations help cover website hosting, software licensing, domain costs, and the time involved in creating and maintaining tutorials, courses, and learning resources. Your support helps keep this content freely available to everyone and allows new courses and materials to be developed for the GIS community. If the content has helped you solve a problem, learn a new skill, or advance your career, please consider supporting the project with a donation. Every contribution, no matter the size, is greatly appreciated.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					import arcpy

################################################################################
## ArcPy Reference Links:
##  https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/get-count.htm
##  https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/result.htm
##
################################################################################

################################################################################
## USER INPUT ##################################################################

## the path to the feature class to get the feature count
fc_path = r"C:\path\to\fc_or_table"

################################################################################
## GET COUNT ###################################################################

feature_count = arcpy.management.GetCount(
    in_rows = fc_path
)

print(feature_count)
#print(int(feature_count)) ## this will fail (TypeError) cannot cast Result to int
print(feature_count[0])
print(type(feature_count[0])) # number is a string
print(int(feature_count[0])) # cast to integer
print(type(feature_count)) # Result object returned from geoproccessing tool.
print(feature_count.outputCount) # GetCount returns one output
print(feature_count.getOutput(0)) # access the output via the Result object

################################################################################
print("\nSCRIPT COMPLETE")
				
			

Leave a Comment