# Creating new field in ArcGIS Desktop which contains values from two different columns with certain conditions?

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

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?

Python has ternary conditional operator:

``````!B! if !A! == 25 else !A!
``````
• Simplest the best, no pre-logic script, no argument ordering. Dec 5, 2018 at 8:03

You can use Field Calculator using Python Parser:

in the Pre-Logic Script write the following code:

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

C=

``````getValue(!A! , !B!)
``````

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]
else:
row[2] = row[0]
cursor.updateRow(row)
``````
• Thank you. But i wasn't sure where to input this code.
– nish
Dec 5, 2018 at 6:43

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
else:
return fieldA
``````

C =

``````CalcColumn(!A!, !B!)
``````

References: