1

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?

4 Answers 4

6

Python has ternary conditional operator:

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

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
    else:
        return f1

C=

getValue(!A! , !B!)
3

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)
1
  • 1
    Thank you. But i wasn't sure where to input this code.
    – nish
    Dec 5, 2018 at 6:43
2

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:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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