Select to view content in your preferred language

Replace Null Value - Field Calc, Python

5889
3
Jump to solution
10-01-2018 11:13 AM
KeithSoRelle
Occasional Contributor

Trying to replace a null value with the field calculator. I am have a script that appends multiple feature classes into one feature class. After each feature class appends I want to add a field calculateField_management (field calculator) statement run that makes all null values from a specific field into a string value. 

I've tried a number of ways but have yet to get an expected result. 

I have 2 values for in the field - Null, 'CFW' and need to replace Null with 'TAR'.

Then I will have 3 values - Null, 'CFW', 'TAR' and will need to replace Null with 'DEN'

I've been trying something along the lines of:

 

def myCalc(x)

      if (x != 'CFW"):

         return 'TAR'

else:

      return 'CFW'

Should be simple but I haven't had much luck getting the expected response. Could use help on both field calculations.

Thanks,

-Keith 

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

You can use Python Conditional expressions, a.k.a., ternary operators, to accomplish this task on one line.  Set the parser to Python and put the following in the expression box:

'TAR' if !Field! is None else !Field!

Swap !Field! above for your field.

View solution in original post

3 Replies
DanPatterson_Retired
MVP Emeritus
if x is None:

    do something

should work if it is truly none and not just an empty string

or to cover all bases (add more to the tuple if needed

if x in (None, "", " ", "nadda"):

    do something 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You can use Python Conditional expressions, a.k.a., ternary operators, to accomplish this task on one line.  Set the parser to Python and put the following in the expression box:

'TAR' if !Field! is None else !Field!

Swap !Field! above for your field.

KeithSoRelle
Occasional Contributor

Worked well. Thanks for the help!

0 Kudos