# Replace Null Value - Field Calc, Python

4518
3
10-01-2018 11:13 AM
New Contributor II

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

Tags (4)
1 Solution

Accepted Solutions
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.

3 Replies
MVP Legendary Contributor
``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

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.

New Contributor II

Worked well. Thanks for the help!