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

Running ArcGIS 10.1 for Desktop, I have two feature classes in a geodatabase. One is a point FC for polling stations, the other is a polygon FC for states.

The polling station FC contains over 1000 points and has a states field indicating which state the polling station is located in. The states FC has an attribute for each state and has a blank field for the number of polling stations in each state.

I need a python script that will get the count of how many polling stations are located in each state from the polling station FC, and then write those numbers to the column in the states FC - obviously to their corresponding state.

-
Would you be able to edit your Question to include the GIS software and version that you are using, please? – PolyGeo Feb 25 '14 at 0:16

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.

-
Thank you, this has helped a lot! It seems that something with the 'SelectLayerByAttribute' is not working properly. I did notice that the where clause was in the incorrect location (it should come after 'NEW_SELECTION') but this didn't solve the problem. When I execute the script after switching the location of the where clause, I get an invalid expression error. – Carto Feb 25 '14 at 15:03
Sorry about that, you're right about the order I'll update that in my answer. I think the "invalid expression" is due to some missing quotation marks that I left out of the where statement. I'll fix that up as well...let me know how it works for you. – Jason Feb 25 '14 at 16:08
That did it - thanks so much Jason! – Carto Feb 25 '14 at 16:36

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.

-

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.
-
The only possible issue with using the Frequency tool instead of the Summary Statistics tool is that Frequency is (ridiculously) only available with an ArcInfo/Advanced license. – RyanDalton Feb 26 '14 at 1:43

To bring another not Python-coding-based solution: You could use the tool 'Tabular Intersection' to get an Output table by states with the number of intersecting polling stations. This table again, can be joined to the states Shapefile to add the attribute column with the number of polling stations.

-