Apr-24-2017, 02:33 PM
I am working on a project where I want to add a new field to a shapefile. This script will be added to a toolbox to be run in ArcCatalog so i will need to use arcpy and sys (possibly).
I have an excel file with election data (votes by county for virginia for 2008, 2012, and 2016) that i have joined with a VA counties shapefile.
I want to add a new field that looks at the value in one field and compares to the value in the adjacent column (i.e. compare republican votes to democratic votes to determine which party won in that county for that specific year).
This is what i have so far but it is not working. Not quite sure what else to try.
I have an excel file with election data (votes by county for virginia for 2008, 2012, and 2016) that i have joined with a VA counties shapefile.
I want to add a new field that looks at the value in one field and compares to the value in the adjacent column (i.e. compare republican votes to democratic votes to determine which party won in that county for that specific year).
This is what i have so far but it is not working. Not quite sure what else to try.
import arcpy import sys inFC = sys.argv[1] field = sys.argv[2] newField = arcpy.AddField_management(inFC, field, "Candidate_Winner") arcpy.AddMessage("New field was created") cursor = arcpy.UpdateCursor(inFC) for row in cursor: democValue = row.getValue("Democratic_2016") repubValue = row.getValue("Republican_2016") if democValue > repubValue: row.setValue(field, "Clinton") cursor.updateRow(row) arcpy.AddMessage("Row was updated") else: row.setValue(field, "Trump") cursor.updateRow(row) arcpy.AddMessage("Row was updated") del row del cursor