I am working in ArcMap 10.2 and would like to calculate a field.

The output should be the value of 'field 1' only if 'field 1' has a value 0 or it should give the value of 'field 2'.

How to do this with the calculate field tool, python?


In the code block window, using python as parser:

def my_calculate(field1_value, field2_value):
  if field1_value == 0:
    return 0
    return field2_value

In the expression window:

my_calculate(!field1!, !field2!)

This could also be done by using the "Select Features by Attribute" and selecting all the features with 0 in field 1, calculating the target field to 0, then invert the selection and calculate the target field to the value of field2. You have the advantage of seeing how many values of each there are and possibly spotting issues right away.

  • That worked out fine with a small adjustement, thanks! – Danielle Apr 2 '15 at 13:42

If you wanted to do this and not bother with the code block, just express this as a logical Python statement in the expression parameter.

{!field2! if !field1! is not 0 else !field1!}

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.