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?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityIn the code block window, using python as parser:
def my_calculate(field1_value, field2_value):
if field1_value == 0:
return 0
else:
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.
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!}