arcpy.MakeFeatureLayer_management(FC, "FC_lyr") FC_lyr = "FC_lyr" arcpy.AddJoin_management(FC_lyr, "joinField", joinedFCTable, "joinField") fields =("{}.DataField".format(FC_lyr), "{}.OtherDataFiedl".format(joinedFCTable)) where = '"{}.OBJECTID" IS NOT NULL'.format(joinedFCTable) with arcpy.da.UpdateCursor(FC_lyr, fields, where) as rows: for row in rows: row[0] = row[0] + row[1]
Solved! Go to Solution.
where it is working there are the same number of records in the update and join tables (one record in each table). Where it isn't working, the join table only has a few matches in the update table
lutDict = dict([(r[0], (r[1], r[2])) for r in arcpy.da.SearchCursor(lutTbl, ["SOURCEJOINFIELD","SOURCEFIELD1","SOURCEFIELD2"])]) updateRows = arcpy.da.UpdateCursor(targetTbl, ["TARGETJOINFIELD","TARGETFIELD1","TARGFETFIELD2"]) for updateRow in updateRows: joinFieldValue = updateRow[0] if joinFieldValue in lutDict and joinFieldValue != None: updateRow[1] = updateRow[1] + lutDict[joinFieldValue][0] updateRow[2] = updateRow[2] * lutDict[joinFieldValue][1] else: updateRow[1] = -9999 updateRow[2] = -9999 updateRows.updateRow(updateRow) del updateRow, updateRows
lutDict = dict([(r[0], (r[1], r[2])) for r in arcpy.da.SearchCursor(lutTbl, ["SOURCEJOINFIELD","SOURCEFIELD1","SOURCEFIELD2"])]) updateRows = arcpy.da.UpdateCursor(targetTbl, ["TARGETJOINFIELD","TARGETFIELD1","TARGFETFIELD2"]) for updateRow in updateRows: joinFieldValue = updateRow[0] updateRow[1] = lutDict[joinFieldValue][0] updateRow[2] = lutDict[joinFieldValue][1] updateRows.updateRow(updateRow) del updateRow, updateRows
Thanks for posting this. I needed something similar for the results of the Near Analysis and getting the 'near' attributes back to the original feature without using AddJoin_management.
nearDict = dict([(r[0], (r[1])) for r in arcpy.da.SearchCursor(XFMR, ["OBJECTID","EQUIPMENT_ID"])]) with arcpy.da.UpdateCursor(TIGER_Place_Layer, ["NEAR_FID","EQUIPMENT_ID"]) as cursor: for row in cursor: joinFieldValue = row[0] row[1] = nearDict[joinFieldValue] cursor.updateRow(row) del cursor, row
Thank you both for your prompt responses. These examples are very helpful. I've been able to get this working to update field values using joined values, but my understanding of dictionaries is pretty rudimentary at this point... Do either of you have any hints or example of how to do some simple arithmetic? My two calculations are simply
updateValue = joinValue + updateValue
and
updateValue = joinValue * updateValue
I have yet to get the syntax right using the dictionary method. Thanks,
-Erik
Would I make 2 dictionaries from 2 SearchCursors? One on the update data and a second on the joined data?
lutDict = dict([(r[0], (r[1], r[2])) for r in arcpy.da.SearchCursor(lutTbl, ["SOURCEJOINFIELD","SOURCEFIELD1","SOURCEFIELD2"])]) updateRows = arcpy.da.UpdateCursor(targetTbl, ["TARGETJOINFIELD","TARGETFIELD1","TARGFETFIELD2"]) for updateRow in updateRows: joinFieldValue = updateRow[0] updateRow[1] = updateRow[1] + lutDict[joinFieldValue][0] #assuming these values are numeric and not strings or anything! updateRow[2] = updateRow[2] * lutDict[joinFieldValue][1] #assuming these values are numeric and not strings or anything! updateRows.updateRow(updateRow) del updateRow, updateRows
Not sure why you would need to make two dictionaries (maybe if you had two look up tables?).
Dictionaries are a great way to store tabular information... with the concept that you have a "key" field and then a bunch of other values that are associated with the key... sounds like a RDBMS table, right?
dict = {}
dict["cat"] = [1, "fish", 5.7654]
dict["dog"] = [3, "garbage", 15.5]
In this case the key is a string "cat", which is associated with a list of values (age, diet, and weight maybe).
Keys must be unique, but can be strings, integers, floats, or even "tuples" such as a value of (1,2). Tuple keys can be extreemly usefull.
You retreive values like this:
#get the diet of "cat"
>>> print dict["cat"][1]
'fish'
Dictionaries are great since they are EXTREEMLY fast to access, but a downside is that their size is limited by how much RAM you have.
Anyway to do math per my earlier example:lutDict = dict([(r[0], (r[1], r[2])) for r in arcpy.da.SearchCursor(lutTbl, ["SOURCEJOINFIELD","SOURCEFIELD1","SOURCEFIELD2"])]) updateRows = arcpy.da.UpdateCursor(targetTbl, ["TARGETJOINFIELD","TARGETFIELD1","TARGFETFIELD2"]) for updateRow in updateRows: joinFieldValue = updateRow[0] updateRow[1] = updateRow[1] + lutDict[joinFieldValue][0] #assuming these values are numeric and not strings or anything! updateRow[2] = updateRow[2] * lutDict[joinFieldValue][1] #assuming these values are numeric and not strings or anything! updateRows.updateRow(updateRow) del updateRow, updateRows
This makes perfect sense
#print each key and it's 2nd associated value in the dictionary (2nd value is index 1) for key in dict: print str(key) + " - " + str(dict[key][1])