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!)
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Feb 3, 2021 at 14:38
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! 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
answered Feb 3, 2021 at 15:14
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)
Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
answered Aug 25, 2022 at 19:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.