Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I would like to calculate a field (short integer) using other fields (short integer) present in the table. In some cases these fields may have null values for one or more of the fields. For example, the new field name is 'total' and total is based upon adding field1 + field2 + field3. Some records (rows) have values for field1, field2, and field3, but some rows only have values for field1 and field2, then field3 is null. Do I need to go and replace all null values with a numeric value? Or, is there some workaround within the VB or python script?

I'm using ArcMap 10.1

Thanks

-al

I've used a python example from below and have been unable to get it to work. Here is the code I'm using:

#calculate new field 'summed' in feature class 'test'
inFeatures='test'
fieldName01='summed'
exSummed = "stack(!field01!+!field02!+!field03!)"
codeBlock = "def stack(*args):
    return sum(filter(None, args))"

arcpy.CalculateField_management(inFeatures, fieldName01, exSummed, "PYTHON", codeBlock)

where 'summed' is the field (short) I would like to populate and field01, field02, field03 are all short integers which may or may not have NULL's present.

I receive an error when I run this...

enter image description here

I'm sure it's something simple I'm doing wrong. Thanks-al

share|improve this question
add comment

3 Answers

A nice Pythonic way of tackling this is to use the filter function, which takes two inputs: the value you'd like to exclude, and the input object (any interable). After that, just sum the result:

def stack(*args):
    return sum(filter(None, args))
share|improve this answer
    
+1 for the single line solution –  Jason Jun 4 '13 at 15:24
    
This is a great answer "scw". I'm still finding my way in Python and this is a really neat way of summing up the contents of a list whilst skipping no data. –  Hornbydd Jun 4 '13 at 15:32
add comment

@ustroetz answer is almost correct. However, it won't necessarily give you the correct results.

When you mutate the list while iterating over it, you may skip some values.

For example, if your list is defined as such:

mylist = [None, 3,4,5,6,7, None, None, 4,65,23,6, None]

and you use this code:

for item in myList:
  if item is None:
   myList.remove(item)

Then your resulting mylist will be:

[3, 4, 5, 6, 7, 4, 65, 23, 6, None]

Please see this post and this post (I asked the second question when I tried to replicate the above code) on Stackoverflow which cover similar issues.

@Lattyware's response describes using list comprehension to get the correct answer.

So, if we modify @ustroetz's answer, you should put the following as your pre-logic code:

def stack(*args):
  myList = list(args)
  myList [item for item in mylist if item is not None]
  return sum(myList)
share|improve this answer
    
+1 for the example - I didn't realize that could happen...I like the [x for x in y] operation. –  Jason Jun 4 '13 at 15:25
add comment

The following python field calculator code should do the job:

total =

stack( !field1!, !field2!, !field3!)

Pre-Logic Script Code:

def stack(*args):
 myList = list(args)
 for item in myList:
  if item is None:
   myList.remove(item)
 return sum(myList)
share|improve this answer
    
mutating the list while iterating it will possibly skip results. See my answer below. –  Fezter Jun 4 '13 at 6:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.