AnsweredAssumed Answered

Update values  in attribute table

Question asked by agr_wisc on Nov 15, 2017
Latest reply on Nov 15, 2017 by agr_wisc

I have three columns  (other than OID FID) in my shapefiles for river. And I want to update "from node" and "to node column"

Before update

from node    to node    gridcode   
2            3          8
3            2          9
7            2          5

After update, I want "node column" to be same as "grid code" column, so I used field calculator in ArcGIS (from node = gridcode). My "from node" column has been changed to gridcode but not sure how to update "to node" column (that depends on values in “from node” column)

Below is the final result I want:

from node    to node    gridcode 
8            9          8
9            8          9
5            8          5
Half of the code is working  
import arcpy, os, sys    
from arcpy import env    
fc
=r"C:/Users/wis/Desktop/python/fe/fea_c.shp"   
# instantiate empty dictionary   
#'FROM_NODE','GRID_CODE','TO_NODE' name of columns in my attribute table    
 node_values = {}   
# build dictionary of values   
with arcpy.da.SearchCursor(fc, ['FROM_NODE','GRID_CODE']) as cursor:
     for row in cursor:
           old_node = row[0]
           new_node= row[1]           
           node_values
[FROM_NODE] = new_node     
with arcpy.da.UpdateCursor(fc, ['FROM_NODE','TO_NODE','GRID_CODE']) as cursor:       
    for row in cursor:            # set fromnode to gridcode value           
         row
[0] = row[2]            # set tonode to new equivalent node           
         to_node
= row[1]# I get an error here           
         new_to_node
= node_values[TO_NODE]           
         row
[1] = new_to_node            
         cursor
.updateRow(row)

The error is because row[1] or "to node" does not have all numbers that are in "from node" or "grid code" Because some values will repeat in t"no node" because one river can be connected to more than one rivers. However no repetition in "gridcode" and "from node" Any suggestions

Attachments

Outcomes