Select to view content in your preferred language

Copy values from one field to another from duplicate record

398
1
Jump to solution
10-27-2022 11:27 AM
TimHayes3
Occasional Contributor

I have a point feature class. 

In this screenshot you will see that ADDR_KEY contains duplicate values. I want to move the values in CROSS_ST_NAMES field to CS1StreetName field and CS2StreetName field as follows:

ADDR_KEY = 3460 (1st occurrence) then move CROSS_ST_NAMES to CS1StreetName

[the value that would be placed in CS1StreetName field would be GABLE DR/MCDUFF AVE]

 

then ADDR_KEY = 3460 (2nd occurrence) then move CROSS_ST_NAMES to CS2StreetName

[the value that would be placed in CS2StreetName field would be FUSTERIA CT/MCDUFF AVE]

and so on for the other ADDR_KEY values...

 

any ideas how to do this in python or with a geoprocessing tool? I am new to python. 

CrossStreetScreenshot.PNG

0 Kudos
1 Solution

Accepted Solutions
DonMorrison1
Occasional Contributor III

I would make 2 passes through the table. First with a search cursor to find and record the duplicates, second with an update cursor to update the two fields as needed.  Something like this......

addrkey_to_streets = dict()
with arcpy.da.SearchCursor(table, ['ADDR_KEY', 'CROSS_ST_NAMES']) as cursor:   
    for addrkey, street in cursor:
        if addrkey not in addrkey_to_streets.keys():
            addrkey_to_streets[addrkey] = {'first': street, 'second': None}
        else:
            addrkey_to_streets[addrkey]['second'] = street

with arcpy.da.UpdateCursor(table, ['ADDR_KEY', 'CS1StreetName', 'CS2StreetName']) as cursor:
    for addrkey, cs1_street, cs2_street in cursor:
        cursor.updateRow([addrkey, addrkey_to_streets[addrkey]['first'], addrkey_to_streets[addrkey]['second']])

 

View solution in original post

1 Reply
DonMorrison1
Occasional Contributor III

I would make 2 passes through the table. First with a search cursor to find and record the duplicates, second with an update cursor to update the two fields as needed.  Something like this......

addrkey_to_streets = dict()
with arcpy.da.SearchCursor(table, ['ADDR_KEY', 'CROSS_ST_NAMES']) as cursor:   
    for addrkey, street in cursor:
        if addrkey not in addrkey_to_streets.keys():
            addrkey_to_streets[addrkey] = {'first': street, 'second': None}
        else:
            addrkey_to_streets[addrkey]['second'] = street

with arcpy.da.UpdateCursor(table, ['ADDR_KEY', 'CS1StreetName', 'CS2StreetName']) as cursor:
    for addrkey, cs1_street, cs2_street in cursor:
        cursor.updateRow([addrkey, addrkey_to_streets[addrkey]['first'], addrkey_to_streets[addrkey]['second']])