Update values in attribute table

3731
15
11-15-2017 12:12 PM
ammaratalib
New Contributor II

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

15 Replies
ammaratalib
New Contributor II

Thanks Xander. It makes sense now. I will use this code to make changes in my subbasin shape files too. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

can you check my logic so I can see if I can produce a vectorized solution using an arcpy.da.ExtendTable operation ...

EDITED to fix the reference to fix_me array

fix_me = np.array([1, 2, 3, 4, 5, 2, 2, 3, 6, 4, 5, 7])
is_good = np.copy(fix_me)
from_ = np.array([2, 1, 3])
to_ = np.array([3, 4, 2])
for i in range(len(from_)):
    is_good[np.where(fix_me == from_[i])[0]] = to_[i]

# ---- compare the results, old and new ----

fix_me
array([1, 2, 3, 4, 5, 2, 2, 3, 6, 4, 5, 7])
i
s_good
array([4, 3, 2, 4, 5, 3, 3, 2, 6, 4, 5, 7]

can you check my logic so I can see if I can produce a vectorized solution

0 Kudos
ammaratalib
New Contributor II

# on line 10 before equal sign there should be "no_" instead of "tonode" (typo i guess)
because you have not defined tonode.

#if i make that change code does not produce the result that line 16 is showing
when i run your code i got
is_good
array([2, 2, 3, 4, 5, 2, 2, 3, 6, 4, 5,7])

looks like your code is setting values in "is_good" equal to the last value of "to_" when "to_=from_" or when "to_" is equal to any value "in from_"
if to_!=from_ then no change in values of "is_good"
#That is replacemnet rather than updating values (if on line 10 it's "no_" rather than tonode)
I guessing wording for my question was very confusing to begin with.
But I liked practicing on your code for my own understanding :-). It helped me to understand about replacement
Now going to practice on arcpy:-)

0 Kudos
DanPatterson_Retired
MVP Emeritus

I edited the tonode array to read 'fix_me' array  the previous was a clone, which I renamed for the final example... my bad, hope it makes sense now

0 Kudos
ammaratalib
New Contributor II

yes it is working now. Thanks Dan. I appreciate that. 

0 Kudos
NyigamBole
New Contributor II

Mr. Ammaratalib! I am having the same issue here. can you share with me the python code as to how you could achieve your target! Will appreciate it a lot as I am newly learning python.

0 Kudos