So I have several field in an attribute table that have <Null> value, I am wanting to using the 'Calculate Field' GP tool to select the <Null> values and replace them with zero. Anyone have any ideas on the Python and/or SQL statement I would use to execute this?
The following field calculator Python expression (no Code Block necessary) should work for you:
<SPAN class="number token">0</SPAN> <SPAN class="keyword token">if</SPAN> !field! <SPAN class="keyword token">is</SPAN> None <SPAN class="keyword token">else</SPAN> !field!<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
PEP 8 -- Style Guide for Python Code
For reference
<SPAN class="keyword token">if</SPAN> foo <SPAN class="keyword token">is</SPAN> None<SPAN class="punctuation token">:</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
'is' is preferred to '==' as a general recommendation
Would this work for your purposes Chad? Just replace with your field name NRCS_Score or whatever in place of what I have specified for my field CENTERLINEID. This will write Zero to all NULL values:
Result of CENTERLINEID field from <NULL> to 0:
>
EDIT: Here is the code using your NRCS_Score field as example:
NRCS_Score =
updateValue(!NRCS_Score!)
Code Block<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">updateValue</SPAN><SPAN class="punctuation token">(</SPAN>value<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> value <SPAN class="operator token">==</SPAN> None<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="string token">'0'</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> value
Using the tools? No. Using a python script, absolutely. What's your preference?
I am trying to do all of this in one step either using the 'Calculate Field' GP in Pro or even better the 'Calculate Fields' GP tool in Pro. Any idea how that would look?
The sql statement you'll use in the selection :
<YourField> is null
Your field calculator will then look like this:
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.