Count number of attributes for unique field values in ArcGIS for Desktop?

There are a few ways to tackle this, but here's a pretty easy one. I'm going to assume you're using ArcGIS. This can be easily done without python. Have you looked at the Summary Statistics tool? You can run that on your Polling Station layer and join the resulting table back to the State feature class.

Set the Statistics field to State and statistics type to Count. Join the results to the state feature class and update the Count field in that layer.


If you are using ArcGIS I would think an easy way to do this, without creating any intermediary products, would be to select each state in your states polygon (assuming you have multipart features) and then select by location the polling stations features. You can then get a count of how many features are selected and writing this value to your states polygon. This would look something like this:

import arcpy

# Define some variables
#
polling_stations = r'c:\path\to\geodatabase.gdb\polling_stations'
states = r'c:\path\to\geodatabase.gdb\states'

# Make feature layers for processing
#
polling_stations_lyr = arcpy.MakeFeatureLayer_management(polling_stations,r'in_memory\polling_stations_lyr')
states_lyr = arcpy.MakeFeatureLayer_management(states,r'in_memory\states_lyr')

# Create an update cursor to access and update states features
#
fields = ['field_containing_state_names','field_which_will_be_updated_with_#_of_polls']
with arcpy.da.UpdateCursor(states_lyr,fields) as cur:
    for row in cur:

        # Make a query to select this feature to be used in a selection of polling places
        #
        state = row[0]
        where = '"field_containing_state_names" = \'{}\''.format(state)
        arcpy.SelectLayerByAttribute_management(states_lyr,'NEW_SELECTION',where)

        # Now select the polling stations by location using the selected state feature
        #
        arcpy.SelectLayerByLocation_management(polling_stations_lyr,'INTERSECT',states_lyr)

        # Count the number of polling stations selected
        #
        number_of_polling_stations = int(arcpy.GetCount_management(polling_stations_lyr).getOutput(0))

        # Update the state feature with this value
        #
        row[1] = number_of_polling_stations
        cur.updateRow(row)

print('Operation complete.')

This code requires ArcGIS 10.x, but can be made compatible with previous versions by using the older style cursor in place of the data access module cursor used here.


Assuming you are using ArcGIS, you could write a python script to do this, but you could also just do the following:

  1. Intersect the points with the state polygons. This will produce a new point dataset.
  2. Run a the Frequency tool to get a count of the number of points per State field.
  3. Join this to the original state polygon FC and calculate your column.