Is it possible to copy a single value inside a field based on it's OBJECTID from another field?

Some sort of if-then-else logic?

For example I would like to copy the value in the Area field corresponding to OBJECTID 3. And to have it in the first row in the Level field with the rest of it's rows remaining to 'Null'.

I am using ArcGIS Pro 1.4 with Python 3.5.

Field Calculator

target values

  • 2
    Don't believe a look-ahead can be done inside a single field calc expression without some hackery (you could embed a search cursor in the codeblock, for example -- seriously, don't do this). – Paul Jan 13 '17 at 20:14

There are a few ways you can do this - quick code in the Python pane or a cursor in the field calculator are two options I'd look at.

  1. Python pane:
    In the ArcGIS Pro Python pane you can enter the following code to lookup the Area value against a specified ObjectID, and copy that value into the Level field on another specific ObjectID. ("testPoints" is the name of the layer I tested on)

    lookupDict = {1:3, 9:24}
    areaDict = dict()
    fields = ['OID@', 'Area', 'Level']
    with arcpy.da.SearchCursor("testPoints", fields) as cursor:
        for row in cursor:
            areaDict[row[0]] = row[1]
    with arcpy.da.UpdateCursor("testPoints", fields) as uCursor:
        for uRow in uCursor:
            if uRow[0] in lookupDict:
                if uRow[0] in areaDict:
                    myval = areaDict[lookupDict[uRow[0]]]
                    uRow[2] = myval

    enter image description here

    The result in the Attribute Table (sometimes you need to close and reopen the table to see the changes):

    enter image description here

  2. Field Calculator:
    You can use python inside the field calculator reasonably easily (and ArcGIS Pro help pages include examples doing this)

    Here I've used a python dictionary to store the ObjectID I wish to update, and the ObjectID I am looking up. From your question you want to get the Area value from ObjectID 3 and put it into the Level field for ObjectID 1. I have included 9: 24 for example if you wish to use more lookups in the same calculation.

    Expression (on Level field):


    and code-block:

    def lu(oid):
        lookupDict = {1: 3, 9: 24}
        myval = None
        if oid in lookupDict:
            with arcpy.da.SearchCursor("testPoints", ['OID@', 'Area']) as cursor:
                for row in cursor:
                    if row[0] == lookupDict[oid]:
                        myval = row[1]
        return myval

    enter image description here

    And the result in the Attribute table:

    enter image description here

    Depending on the size of your table, you may wish to process the Field Calculator Search Cursor separately (like the python pane example) so you're not running the search cursor on every update cursor row.

  • This is really awesome @Midavalo. I couldn't find any info regarding this topic, I hope it will be of use to everybody – Andrew Mar 6 '17 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.