Suppose I have a attribute table as shown in the image below:

enter image description here

I have columns A and B with common FID. I would like to add a new column C, which contains the values of column A with the condition that if the value is equal to 25 in that column, then for those rows, the value should be assigned from column B.

The highlighted green color column C in the image is how the result should look like.

Is there a tool to do this or does an expression have to be used?


You can use Field Calculator using Python Parser:

enter image description here

in the Pre-Logic Script write the following code:

def getValue(f1,f2):
    if f1 == 25:
        return f2
        return f1


getValue(!A! , !B!)

Python has ternary conditional operator:

!B! if !A! == 25 else !A!
  • @nish, to get more information about a shortcut conditional expression, please check this link. – Taras Dec 5 '18 at 6:51
  • 1
    Simplest the best, no pre-logic script, no argument ordering. – fatih_dur Dec 5 '18 at 8:03

You can use Field Calculator (see examples here) or the da.UpdateCursor like below. Change input and field names and execute in the python window of ArcMap.

import arcpy

fc = r'C:\data.gdb\feature_class' #Change to match your data
fields = ['A','B','C'] #Change to match your data

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        if row[0] == 25:
            row[2] = row[1]
            row[2] = row[0]
  • 1
    Thank you. But i wasn't sure where to input this code. – nish Dec 5 '18 at 6:43
  • 1
    @BERA, have you ever used any GIS with GUI? =) – Taras Dec 5 '18 at 7:05
  • 1
    @BERA, P.S. as always a perfect answer – Taras Dec 5 '18 at 8:14

In the Field Calculator with a usage of Python parser, please type

Pre-Logic Script Code:

def CalcColumn(fieldA, fieldB):
  if fieldA == 25:
    return fieldB
    return fieldA

C =

CalcColumn(!A!, !B!)


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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