I want to use the field calculator to get the sum of a couple of columns. The problem is that some of them has Null Values. I am not able to find a solution. I tried this Python functions:
stack( !basic.Blue_Thickness!, !basic.Green_Thickness!, !basic.Red_Thickness!, !basic.F1_Thickness! )
Version 1:
def stack(*args):
myList = list(args)
myList = [item for item in myList if (item is not None)]
return sum(myList)
Version 2:
def stack(*args):
return sum(filter(None, args))
Version 3:
def stack(*args):
myList = list(args)
for item in myList:
if item is None:
myList.remove(item)
return sum(myList)
The result of all this functions is that I get always NULL back, only if there is a row with no NULL then I get a result.
Background: I have a File Geodatabase with one main table and some tables which joins this table. The calulation takes place only in the main table (source columns and write column). I am using ArcGIS 10.0. The Source of these functions is this discussion: Calculating field in which null values may be present?
2 Answers 2
I just ran the test below in the Field Calculator and it seemed to work.
def stack(item1,item2,item3):
itemList = [item1,item2,item3]
myList = [item for item in itemList if (item != None)]
return sum(myList)
This was the Field Calculator settings I used:
enter image description here
and this is the test table with the result:
enter image description here
I think the only thing that I am not doing at the moment is joining through to another table but before I test that (I am using ArcGIS 10.1 SP1) perhaps you can confirm that this simple test is working for you. It's not as Pythonic but seems to work.
There appears to be a problem with either earlier versions of Arc (I use 10.0) or some installations of Arc where anytime your Python script sees a <Null>
, <Null>
is returned. I am aware of the if myfield is None
approach, but this did not work for me, or a commenter here, or several other users with similar problems in gis.stackexchange. No matter the various clever manipulations/if-elifs, the field is set to <Null>
if a <Null>
is encountered at all in the script.
The only solution that works for me is to use VB Script instead of Python. The isNull(myfield)
function reliably checks if a field is <Null>
without any of the problems above. It is not too difficult to translate simple Python to VB Script.
-
Welcome to GIS SE! Please note that duplicate answers are against Stack Exchange policy (i.e. in reference to: gis.stackexchange.com/a/151333/8104). More details on that subject here: meta.stackexchange.com/q/1042272015年06月17日 21:19:56 +00:00Commented Jun 17, 2015 at 21:19
Explore related questions
See similar questions with these tags.
if item
is the same asif (item is not None)
and easier to read. Likewiseif not item
is the same asif item == None
. I'm not sure if this has any bearing on your Q as I can't test in Arc10 or earlier. FWIW, v1 returns extra Nulls in Arc10.3 also, v3 appears to work properly but beware because mutating a list in place causes unexpected but not immediately obvious mistakes in the results (see here). Bottom line: v2 works, don't use the others.