-1

I am trying to write a script that finds difference but every time I use the information from instructor, I am getting an error, and getting nowhere...

table: field one is: Area_name, field two is 1990 POP, field 3 is pope 2000, and field four is pop 2010...

using ArcCatalog, I created a database located at C:/EsriPress/Script_4/US_County.gbd

broken script:

import arcpy
arcpy.env.workspace="C:/EsriPress/Scripting_4/US_County.gbd"
arcpy.env.overwriteOutput = True
fc = "US_County"# database
with arcpy.da.UpdateCursor(fc, ["Area_Name", "Resident_Population_1990", "Resident_Population_2010",
                                    "SHAPE@Resident_Population_2010", "SHAPE@Resident_Population_1990"]) as cursor:
    for row in cursor:
        Area_Name= row[0]
        x=row[1]
        y=row[3]
        row[4]=x
        row[5]=y
        cursor.updateRow(row)
del cursor

Can someone help?

3
  • 1
    Are you trying to create two new fields and add differences to these?
    – Bera
    Commented Feb 2, 2018 at 18:17
  • I am trying to create field and add it in Commented Feb 2, 2018 at 18:20
  • What is the full error message that you get from running the precise code that you have presented?
    – PolyGeo
    Commented Feb 2, 2018 at 19:44

1 Answer 1

1

It is a bit unclear what you want to do but you cant add new fields using the UpdateCursor, you need to use Add Field.

The shape tokens, like SHAPE@, or SHAPE@XY is for example when you need to modify geometries, or retrieve information about them. You should not need them for your problem.

I am assuming you want to add two new fields and populate with the differences of three existing fields:

import arcpy

arcpy.env.workspace=r"C:\EsriPress\Scripting_4\US_County.gbd"
arcpy.env.overwriteOutput = True
fc = "US_County"

fields_to_create = ["Diff_1990_2000", "Diff_2000_2010"]

for field in fields_to_create:
    arcpy.AddField_management(in_table=fc, field_name=field, field_type='DOUBLE')

input_fields = ["Pop_1990","Pop_2000","Pop_2010"] #Change to match the name of the existing fields

with arcpy.da.UpdateCursor(fc, input_fields+fields_to_create) as cursor:
    for row in cursor:
        row[3] = row[1]-row[0]
        row[4] = row[2]-row[1]
        cursor.updateRow(row)
5
  • So here is the code that I am getting: Traceback (most recent call last): File "C:/EsriPress/Scripting_4/Helped1.py", line 9, in <module> arcpy.AddField_management(in_table=fc, field_name=field, field_type='DOUBLE') File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\management.py", line 3443, in AddField raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Table: Dataset US_County does not exist or is not supported Failed to execute (AddField). Commented Feb 2, 2018 at 18:53
  • Then you have an invalid path or feature class name
    – Bera
    Commented Feb 2, 2018 at 18:55
  • I am not sure why it will not refer back to the "US_Country". It is saved in a geodatabase named US_County.gbd, which I verified. I used ArcCatalog to make the geodatabase and import the file. It is not open anywhere on the computer, so I am not sure what is being done wrong. Commented Feb 2, 2018 at 18:56
  • Okay. I will try to redo the feature class Commented Feb 2, 2018 at 18:57
  • Thank you so much... I was able to get it done by redoing my geodatabase. Commented Feb 2, 2018 at 19:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.