Select to view content in your preferred language

Calculate Field Help (Python and/or SQL): ArcGIS Pro

1501
6
11-26-2019 06:26 AM
chadhickman
Occasional Contributor III

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?

Tags (2)
0 Kudos
6 Replies
JoeBorgione
MVP Emeritus

The sql statement you'll use in the selection :

<YourField> is null

Your field calculator will then look like this:

That should just about do it....
chadhickman
Occasional Contributor III

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?

0 Kudos
JoeBorgione
MVP Emeritus

Using the tools?  No.  Using a python script, absolutely.  What's your preference?

That should just about do it....
0 Kudos
by Anonymous User
Not applicable

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:

Field Calc Null


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:
def updateValue(value):
  if value == None:
   return '0'
  else: return value‍‍‍‍
DanPatterson_Retired
MVP Emeritus

PEP 8 -- Style Guide for Python Code 

For reference

if foo is None:

'is' is preferred to '==' as a general recommendation

JoshuaBixby
MVP Esteemed Contributor

The following field calculator Python expression (no Code Block necessary) should work for you:

0 if !field! is None else !field!