# Name: JoinAttributesFromPolygon.py
# Description: Adding police precinct id and name to arrests, then printing the number of arrests by precinct.
# import system modules
import arcpy
# Set environment settings
arcpy.env.workspace = r"C:\data\city_pd.gdb"
# Set local variables
target_features = "Arrests"
in_features = "Precincts"
join_fields = ['districtid', 'name'] # Police Precinct ID and Name
# Execute JoinAttributesFromPolygon
arcpy.ca.JoinAttributesFromPolygon(target_features, in_features, join_fields)
#Print count of arrest by precinct
count_dict = {}
with arcpy.da.SearchCursor(target_features, 'name') as cursor:
for row in cursor:
try:
count_dict[row[0]] += 1
except:
count_dict[row[0]] = 1
for precinct, count in count_dict.items():
print("Name: " + precinct + " Arrests: " + str(count))