Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a New Field
#1
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.



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
Reply


Messages In This Thread
Adding a New Field - by swahlay13 - Apr-24-2017, 02:33 PM
RE: Adding a New Field - by sparkz_alot - Apr-25-2017, 01:19 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020