Replace values of None in Field Calculator?

1040
3
08-02-2011 07:44 PM
NickJacob
New Contributor II
Hi,

Can't seem to figure this one out.  I'm calculating a field with the field calculator, but not every row is populated with a value. Only about half are populated and the rest of the rows are Null.  I tried to define a python function to replace the Null values with 0, but no luck so far.  It looks something like this:

#Code Block

def popNull(x):
  if x is Null:
    return 0

#Expression

= popNull(!Field!)


I suppose I should have used an update cursor?  As a work around I populated the entire field with a value of 0 before doing my real calc.  That filled in the gaps, but I'd still like to figure this out.
Tags (2)
0 Kudos
3 Replies
StacyRendall1
Occasional Contributor III
Arc NULL is None in Python, so you need this:
#Code Block

def popNull(x):
  if x is None:
    return 0

#Expression

= popNull(!Field!)


The error would have told you: global name 'Null' is not defined (if you hunted through the results window), that indicates to you that Null isn't right...
0 Kudos
NickJacob
New Contributor II
Thanks for responding:)

Well, I just used the word "null" in the name of the function.  It actually runs without any gp errors/results, but nothing happens.  I'm guessing an if statement wasn't the right way to go.  Maybe an update cursor?  or could I have converted the field to a string, used .replace(), then converted back to an integer?
0 Kudos
NickJacob
New Contributor II
Oh, duh.  I see what you're saying.  What I MEANT to say was.. yup, I did switch over to the word None after digging through the forums a bit.  Then, it runs but nothing happens.  The conclusion I've come to is that my approach was all wrong in the first place.

a) simplest solution was to populate the whole field with a value of 0 first.
b) if I wanted to hunt for values of None row by row, then using an update cursor in a stand alone script would likely be the way to go.
0 Kudos