ArcPy/Python – Printing Number of Selected Features Based on Different Selection Criteria in ArcPy

arcgis-proarcpy

I'm working with Parcel feature class in ArcGIS Pro 2.2 and trying to figure out the number of parcels that fit into each category. And each category consists of two criteria:

  • The development ratio of the total parcel
  • The acreage of the parcel

The input data table:

INPUT

The circled fields are the criteria I am using. And the specific classes are shown in the result spreadsheet screenshot below.

This is the result I generated:

Selection Result

I ran the arcpy script for each individual category:

arcpy.SelectLayerByAttribute_management('ParcelBoundaries_DevTest', 'NEW_SELECTION', '"Developed" > 0.25 and "Developed" <= 0.5 and "GISACRES" <= 1')

arcpy.SelectLayerByAttribute_management('ParcelBoundaries_DevTest', 'NEW_SELECTION', '"Developed" > 0.5 and "Developed" <= 0.75 and "GISACRES" <= 1')

arcpy.SelectLayerByAttribute_management('ParcelBoundaries_DevTest', 'NEW_SELECTION', '"Developed" > 0.75 and "Developed" <= 1 and "GISACRES" <= 1')

After each run, I will see the number of the selected features in the attribute:

Selected

Then I will manually input the number into the spread sheet. This is still very tedious. Is there any way that I can use Python or ArcPy to generate the number of the selected features?

Best Answer

Get Count.

Returns a Result object, whose first output is the count.

Thus to get your count:

count = arcpy.GetCount_management ('ParcelBoundaries_DevTest').getOutput (0)

For whatever reason the return value is a string, so if you want a number just use int.

count = int (arcpy.GetCount_management ('ParcelBoundaries_DevTest').getOutput (0))