2

I'm using ArcGIS Pro, the field calculator. I want to populate a hydrant layer with hydrant inspection data. I have two fields from a joined table: field 1 and field 2. I want to populate field 1 with the values from field 2, but only if the values ARE NOT Null. Basically I want to preserve the values in field 1 if the corresponding value in field 2 is Null. I've been trying slight variations of the python code below. But it just populates field 1 with all of field 2 whether there are nulls or not.

I'm a beginner when it comes to Python.

Codeblock:

def reclass(f1,f2):
        if f2 is not None:
            return f2
        else:
            pass

Expression:

reclass(!field1!,!field2!)
1
  • This can be done by using a Python conditional expression, a.k.a., ternary operator, in the expression block, no code block needed: !field1! if !field2! is None else !field2!
    – bixb0012
    Commented Aug 25, 2022 at 20:38

2 Answers 2

3

I figured it out!

Expression:

Reclass(!field1!,!field2!)

Codeblock:

 def Reclass(f1,f2):
        if f2 is None:
            return f1
        else: 
            return f2
0

A modified version of Katie's answer if you want to add text to non-null fields

def Reclass(RMNM):
        if RMNM is None:
            return RMNM
        else: 
            return ("RM of " + RMNM)

Your Answer

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

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