Select to view content in your preferred language

Update Fields by Matching Fields

2245
9
10-10-2017 03:54 PM
DevinUnderwood2
Regular Contributor

I want to know what is the best way via arcpy  to update a feature field by referencing another features field.

I don't know if it is possible to use a updatecursor for 2 different features.

Would I just perform a temporary join and use arcpy like field calculator.

My Example of my cursor attempt.

searchcursor = arcpy.SearchCursor("BASE.ROADS_SG_ESC","","","","")
updatecursor = arcpy.UpdateCursor("escroads","","","","")

for row in searchcursor:
    RD20NAME = row.getValue("RD20NAME")

for row in updatecursor:
    if row.getValue("NAME")
        NAME = RD20NAME    
       updatecursor.updateRow(row)
0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

you could join, then update a field... you could do this with cursors, but most people ignore simple field calculations can be accessed through the Calculate Field tool and its associated script code at the bottom

JoshuaBixby
MVP Esteemed Contributor

For starters, look into using the Data Access (What is the data access module?—Help | ArcGIS Desktop ) cursors instead of the original/old cursors.  The DA cursors perform much better and are a bit more Pythonic.

Your code snippet confuses me more than helps me understand what you are trying to do.  Code aside, what are you trying to do?

DevinUnderwood2
Regular Contributor

Thank you for the suggestions.

I will look  into the Data Access module as an alternative to old cursors.

Good advice on Calculate field tool and a join.

I will see what works for me. As I am trying to copy attributes from one field of a feature class to another feature class.

However, I need to match the feature classes fields that have different field names. All of this done via a python script.

0 Kudos
CCWeedcontrol
Frequent Contributor

Here is a code i use for similar situations.

You need to have a uniqueID for both features layers.

from time import strftime  
print "Start script: " + strftime("%Y-%m-%d %H:%M:%S")  
import arcpy

#Transfer of Multiple Field Values between Feature Classes where there is a 1:1 Match between Field Sets
  
sourceFC = "Parcels"

#change OID@ to your uniqueID 
sourceFieldsList = ['OID@', 'Field_1','Field_2', 'Field_3','Field_4']  # Your Source fields  
  
# Use list comprehension to build a dictionary from a da SearchCursor  
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}  
  
updateFC = "Points"  
  
updateFieldsList = ['OID@', 'Field1', 'Field2', 'Field3', 'Field4']  # your updateFC fields
  
with arcpy.da.UpdateCursor(updateFC, updateFieldsList) as updateRows:  
    for updateRow in updateRows:  
        # store the Join value of the row being updated in a keyValue variable  
        keyValue = updateRow[0]  
        # verify that the keyValue is in the Dictionary  
        if keyValue in valueDict:  
            # transfer the values stored under the keyValue from the dictionary to the updated fields.  
            for n in range (1,len(sourceFieldsList)):  
                updateRow = valueDict[keyValue][n-1]  
            updateRows.updateRow(updateRow)  
  
del valueDict  
  
print "Finished script: " + strftime("%Y-%m-%d %H:%M:%S")  
print "Finished script: " + strftime("%Y-%m-%d %H:%M:%S")
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
DevinUnderwood2
Regular Contributor

Thank you, I will try this out.  My unique will be roadsegid 

0 Kudos
DevinUnderwood2
Regular Contributor

This worked perfect. I only need to modify to only update features that have a RecID = 0

I used this and added code to generate a zipfile.

0 Kudos
CCWeedcontrol
Frequent Contributor

Can you post your modified code please.

0 Kudos
DevinUnderwood2
Regular Contributor

I would like to post it after I modify the code some more to use ListFields rather than manually hardcode the field names.

Also, one last section of tying in the max() value to the auto increment () function, which I haven't had a chance to test yet, but soon.

0 Kudos
ChadBunn1
Emerging Contributor

Is it possible to modify this script so that a source field and an update field could have different field types if the values are the still the same?  For example if my source and update fields have a value of '1234', but the source field is an integer and the update field is a string, can they still be used with list comprehension somehow? Or would it be better to just add a corresponding field with the matching field type to my update layer?

0 Kudos