Compare field values from 2 different tables, Locate value from table 2 in that row, use UpdateCursor to Update field in table 1

4680
12
Jump to solution
01-07-2021 09:55 PM
MPach
by
Occasional Contributor II

I'm kind of new to this, but kind of not. I just don't use it as often as I would like in some cases. With that out of the way here's what I'm attempting to do. 

I have two tables that I am working with 1) Manhole Table 2) Gravity Mains Table. The important rows for this script are in the manhole table: INVERTELEV (invert elevation) and FACILITYID (an identifier). From the Gravity Mains table there are 2 important rows: FROMMH (this will be a manhole Facility ID #) and UPSTREAMIN (the upstream invert of the gravity main).

What I am attempting to do is with all the Gravity Main FROMMH values in a list take each value from that list and compare it to a value in the Manhole table FACILITYID field until I find a match. When I find a matching value I want to copy the value in the INVERTELEV field from the manhole table and update the UPSTREAMIN value in the Gravity Mains table. It seems simple enough, but I think I'm making this more complicated than it has to be and I'm confusing myself with nested loops. 

I've attach a python file that I'm working on with some notes. Any help on how to do this would be greatly appreciated. 

FYI, one thing I am kind of unclear about is  with the Update cursor. Does this UpdateCursor object update the data at the current position, meaning the current row it's working on. What exactly does that mean? Does it mean that if I find the row with the matching FACILITYID and FROMMH that when I go to use the row.SetValue()  method it will update that row. That's what it sounded like when I read it, but I'm just trying to clarify. I know I could probably setup a sample to test this, but I figured I might as well ask while I'm posting this request for help.  

Thanks 

 

 

12 Replies
MPach
by
Occasional Contributor II

Awesome, Glad my thought blanking could help someone. That's why this community is here. Cheers!

0 Kudos
JoeBorgione
MVP Emeritus

Here is my basic approach to using a dictionary and an update cursor:

import arcpy

target = r'C:\path\to\the\table_to\update'
upDateFields = ['CommonKey', 'FieldN']

source = r'C:\path\to\the\data_that_has\what_target_needs'
sourceFields = ['CommonKey', 'Field1']
sourceDict = {r[0]:(r[1]) for r in arcpy.da.SearchCursor(source,sourceFields)}

with arcpy.da.UpdateCursor(target,updateFields) as updateRows:
        for updateRow in updateRows:
           keyValue = updateRow[0]
           if keyValue in sourceDict :
               updateRow[1] = sourceDict[keyValue]
               updateRows.updateRow(updateRow)
           else:
                pass

Having poured through the script for a minute or two, it seems like you are over thinking it a bit.  Not a criticism, just an observation.  The basic premise is to create a dictionary of the source data using the common key between the source and the target as the key in the dictionary, and then the associated value in that dictionary is what the target gets updated with.

Lets say the common key between the source and the target is a field called FacilityID .  Using dictionary comprehension create a the dictionary with a search cursor as shown above and it looks something like this:

 

{1:'value1',
 2:'value2',
 3:'value3'}

All you need to do then is look and see if the dictionary key appears in the target, and then used the associated value with that key to update the target field.

A row in a search cursor or update cursor is in the format of a tuple so the first element is row[0]. 

Make sense?

 

 

 

That should just about do it....
0 Kudos
JoeBorgione
MVP Emeritus

Post your code rather than attaching a zip file: click on the three dots to expand the menu tool bar, select the </> icon (insert code) and choose python. That way several sets of eyes can take a look at it...

That should just about do it....
0 Kudos