Hi,
Data in FGDB have Null values by default in numeric fields. When performing calculation, it cannot sum fields where Null values occur.
My question is how to replace all Null values to 0 - in several/all numeric fields - in one go?
Or, potentially, I can use the Calculate Fields tool but what could be an expression to replace Null to 0 on a single line (cannot use expression on multiple line with this tool) ?
On single line, I tried:
if None: 0
if Null: return 0
if None: return 0
replace(Null, 0)
...obviously, none worked!
Thanks
Obviously, first make sure that it is valid to change Null values to 0. Are they really 0, or could they be another value and you just don't know?...
Anyway, if you use the Reclassify helper:
You can then quickly modify it to look something like:
Result:
You can obviously write this yourself - I'm just pointing you to the Helpers, because they're designed to give a kick-start on some of the more common functions...
Your attempts weren't working in with Calculate Field because 3 of the 4 are not valid Python statements and the 4th just doesn't do anything:
if None: 0 # doesn't do anything
if Null: return 0 # SyntaxError: 'return' outside function
if None: return 0 # SyntaxError: 'return' outside function
replace(Null, 0) # NameError: name 'replace' is not defined
What you can use is a Conditional expression — Python 3.7.5rc1 documentation , a.k.a, ternary operator, to do a simple reclassification.
0 if !field! is None else !field!
Thanks for all your suggestions...
Initially, I was asking how to use an expression to replace Null to 0 on a single line in Calculate Fields in order to use a generic expression stored in a model variable in ModelBuilder... All the solutions mentioned above worked but I always have to "hard code" the field name in the expression and this cannot use it as a generic variable in ModelBuiler. It that's make sense.
Ah, I see what you are asking. I don't work with Model Builder.