Select to view content in your preferred language

Field Calculator: ignore NULL value

06-25-2013 03:20 PM
New Contributor
I am using a File Geodatabase and I want to calculate the sum of different coulms. like column5= [column1]+[column2]+[column3]
That works with the Field Calculator very well, but if there is a <Null>, then the result is also NULL.
Is there a way to ignore the Null fields?
(I want to keep the field like they are, I don't want to overwrite them with '0')
thanks for helping
cheers Immanuel
0 Kudos
2 Replies
Esri Contributor
Hi Immanuel,

Have you tried using an update cursor in python? You can write a simple script to update the column you want calculated. For example, I have a feature class with columns f1, f2, f3, and calc. F1, f2, and f3 have a mix of numbers and null values, and calc is the column that I want updated with the calculation of f1 + f2 +f3. Here is my simple python script to update these answers and skip null values. Note that the only value that gets changed is the "calc" value, and print statements are just used as reference.

# Import the arcpy module
import arcpy

# Location of the feature class I want to update
fc = "C:\\Data\\NothingSpecial.gdb\\calc"

# Create a cursor using arcpy data access module
cursor = arcpy.da.UpdateCursor(fc,"*")

# For each row in the feature class:
for row in cursor:
    print "For row: "+str(row)
    # row[2] is the idex position of the f1 column
    if row[2] <> None:
        x = row[2]
        print "x = %d" %(x)
    # row[3] is the idex position of the f2 column
    if row[3] <> None:
        y = row[3]
        print "y = %d" %(y)
    # row[4] is the idex position of the f3 column    
    if row[4] <> None:
        z = row[4]
        print "z = %d" %(z)

    # Now use these values to calculate the "calc" column
    if x:
        calc = x
        print "calc = %d" %(x)
    if y:
        calc = calc + y
        print "calc + y = %d" %(calc)
    if z:
        calc = calc + z
        print "calc + z = %d" %(calc)

    # Set the calc column (row[5]) as the calculated value
    row[5] = calc

    # Update the row with the new value for "calc"

    # Set all values back to 0 for the next row

Here's a link for more information on using Update Cursors:
0 Kudos
New Contributor

Wanted to chime in here 10 years later to note that if you're using arcpro and you switch to an arcade expression (instead of python) in the field calculator it'll successfully read the nulls as zeros. Some classic esri nonsense when I have a deadline to meet, but glad I tried that and wanted to share here.

0 Kudos