Updating a newly generated field with an update cursor

918
2
Jump to solution
06-14-2021 06:58 AM
DanielFuller
New Contributor III

Hello!

 

I have a dataset containing rural-urban data that I need to process.

DanielFuller_1-1623678861803.png

 

my code generates the isUrban field, I then need to use an update cursor to classify that field as "Rural" or "Urban" based off of the RUC11CD field (I'm currently trying to do this as a boolean), if RUC11CD is equal to or greater than "4" it needs to be classified as "Urban" (or true if i stick with boolean) . i've read the documentation on update cursors and watched some videos.

but i've been stuck for 3 days with no idea what i'm doing wrong. my cursor iterates through my table but classifies everything as false (see screenshot). my cursor code currently looks like this

field_Name = "isUrban"
field_Nametype = "INTEGER"
field_NameVal = arcpy.ValidateFieldName(field_Name)

#Adds the new field if it doesn't already exist
fList = arcpy.ListFields(fc,field_Name)
if not fList:
    arcpy.AddField_management(fc, field_Name, field_Nametype, "", "", "")
    sheet = arcpy.management.MakeTableView(Rural)
    fieldcheck = arcpy.ListFields(sheet, "isUrban")

    if len(fieldcheck) == 0:
        arcpy.AddField_management(sheet, "isUrban", 'INTEGER')
    if len(fieldcheck) == 1:
        arcpy.DeleteField_management(sheet, "isUrban")
        arcpy.AddField_management(sheet, "isUrban", 'INTEGER')

fields = ["RUC11CD", "isUrban"]

# Create update cursor for feature class
with arcpy.da.UpdateCursor(Rural, fields) as cursor:
    for row in cursor:
        if fields[0] >3:
            fields[1] = True
        else:
            fields[1] = False
        cursor.updateRow(row)

if i set the new field to be a string and the fields to be 'true' or 'false' as strings the field returns blank cells

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
# you're checking and changing fields, not row!

# if you do this, it should work:
for row in cursor:
    is_urban = row[0] > 3
    cursor.updateRow([row[0], is_urban])

# even shorter:
for row in cursor:
    cursor.updateRow([row[0], row[0] > 3])

 

# Also: you can replace lines 6 to 16 with this
try:
    arcpy.management.DeleteField(fc, "isUrban")
except:
    pass
arcpy.management.AddField(fc, "isUrban", "INTEGER")

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor
# you're checking and changing fields, not row!

# if you do this, it should work:
for row in cursor:
    is_urban = row[0] > 3
    cursor.updateRow([row[0], is_urban])

# even shorter:
for row in cursor:
    cursor.updateRow([row[0], row[0] > 3])

 

# Also: you can replace lines 6 to 16 with this
try:
    arcpy.management.DeleteField(fc, "isUrban")
except:
    pass
arcpy.management.AddField(fc, "isUrban", "INTEGER")

Have a great day!
Johannes
DanielFuller
New Contributor III

you've saved my sanity Johannes I really appreciate it!

DanielFuller_0-1623681583340.png

 

0 Kudos