0

I'm very new to Python and scripting in ArcGIS. I need to calculate some cells from one field in table into another one cell in another field in same table.

Here's what i need:

enter image description here

Here's what i try and it doesnt work.

A = arcpy.da.SearchCursor("S2_merge2", ["Name1"] , 'FID = 0')
B = arcpy.da.SearchCursor("S2_merge2", ["Name1"] , 'FID = 1')
C = arcpy.da.SearchCursor("S2_merge2", ["Name1"] , 'FID = 2')

cursor = arcpy.da.UpdateCursor("S2_merge2", ["Name5"] , FID = 0')

for row in cursor:      
    row[0] = A+B+C   
    cursor.updateRow(row)
3
  • 1
    Do you need to calculate field 'Name5' for all rows or only for the first row of your table?
    – GISGe
    Commented Jan 6, 2016 at 12:43
  • How would you handle naming the last row in the field "Name5"? A complete example of your intended output would be helpful.
    – Aaron
    Commented Jan 6, 2016 at 13:11
  • I need result as shown in picture - only one cell in Name5 should be changed (first row).
    – Fox
    Commented Jan 7, 2016 at 9:06

1 Answer 1

1

first copy the column "Name1" in a python list

features = [[row[0] for row in arcpy.da.SearchCursor("S2_merge2", ("Name1") )]

then use the indices with your search cursor (note that I use a selection to avoid troubles with the last 2 rows, as you did not tell how to manage those)

cursor = arcpy.da.UpdateCursor("S2_merge2", ["Name5"],  'FID < ' + str(int(arcpy.GetCount_management("S2_merge2").getOutput(0))-2) )

i=0
for row in cursor:      
    row[0] = features[i] + features[i+1] + features[i+2]
    cursor.updateRow(row)
    i+=1

with a single row :

fid_val = 42 #42 is just an example
cursor = arcpy.da.UpdateCursor("S2_merge2", ["Name5"],  'FID = ' + str(fid_val) )


for row in cursor:      
    row[0] = features[fid_val] + features[fid_val+1] + features[fid_val+2]
    cursor.updateRow(row)
2
  • as I understand - " .getOutput(0))-2) ) " means that ALL-2 row's will be updated - is there any way to Update only one row? (for exemple i dont know quantity of rows in table)
    – Fox
    Commented Jan 7, 2016 at 9:18
  • you can use any query as third argument , so FID = 0 would dot the trick for a single row. Note that if you start from another FID value than 0, you need to initialize i to this FID value. Also note that getcount will give you the quantity of rows, so you don't have to worry about not knowing it
    – radouxju
    Commented Jan 7, 2016 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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