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
#2
(Apr-24-2017, 02:33 PM)swahlay13 Wrote: This is what i have so far but it is not working.

This is not helpful at all.  You need to explain what is not working. What output are you getting? If you are getting an error, post the entire Traceback between the error tags (the circled red 'x').
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

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