In a counties shapefile, I have two fields named population2001 and population2010. I would like to create a third field with the difference between 2001 and 2010 using arcpy and the update cursor to better learn how to use cursors. My code:
import arcpy
arcpy.env.workspace =
r"C:\Users\Documents\geodatabase.gdb"
ndCounties = r"C:\Users\Documents\geodatabase.gdb\Counties"
#add the new field
arcpy.AddField_management(ndCounties, "difference", "LONG", field_length = 10)
fields = ['POP2001', 'POP2010', 'difference']
with arcpy.da.UpdateCursor(ndCounties, fields) as cursor:
for row in cursor:
if row[0] == 'POP2001':
row[1] = 1
row[2] = 2
elif row[0] == 'POP2010':
row[1] = 2
row[2] = 3
row[3] = row[1] - row[2]
cursor.updateRow(row)
When I run this code, I get this error:
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'
I'm not exactly sure if this is the right way of doing this field subtraction.