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:
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)
-
1Do you need to calculate field 'Name5' for all rows or only for the first row of your table?GISGe– GISGe2016年01月06日 12:43:57 +00:00Commented 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– Aaron ♦2016年01月06日 13:11:54 +00:00Commented Jan 6, 2016 at 13:11
-
I need result as shown in picture - only one cell in Name5 should be changed (first row).Fox– Fox2016年01月07日 09:06:41 +00:00Commented Jan 7, 2016 at 9:06
1 Answer 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)
-
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– Fox2016年01月07日 09:18:59 +00:00Commented 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 itradouxju– radouxju2016年01月07日 13:06:51 +00:00Commented Jan 7, 2016 at 13:06