Script to remove null values from table

This post courtesy of Brett Elliot

Problem:  There is a table with 90% null values, and you want to clean it up.

clip_image001

Solution 1: Using a python script within field calculator.

def RemoveNullString(x):
    if x is None:
        return ''
    else:
        return x

clip_image002

Solution 2:  Scripting using Python.

import arcpy

arcpy.env.workspace = r'T:\Web\MapServices\Server\Harvest\data.gdb'

#functions to remove null string or integer and replace with '' or 0
def RemoveNullString(x):
    if x is None:
        return ''
    else:
        return x

def RemoveNullInt(y):
    if y is None:
        return 0
    else:
        return y

def formatFeatureClass(FC):
    #format string fields
    arcpy.CalculateField_management(FC,"District","RemoveNullString(!District!)","PYTHON_9.3")
    arcpy.CalculateField_management(FC,"Species","RemoveNullString(!Species!)","PYTHON_9.3")

    #format Numeric Fields
    arcpy.CalculateField_management(FC,"Area","RemoveNullInt(!Area!)","PYTHON_9.3")
    arcpy.CalculateField_management(FC,"Harvested","RemoveNullInt(!Harvested!)","PYTHON_9.3")
    arcpy.CalculateField_management(FC,"Non","RemoveNullInt(!Non!)","PYTHON_9.3")
    arcpy.CalculateField_management(FC,"NR","RemoveNullInt(!NR!)","PYTHON_9.3")
    arcpy.CalculateField_management(FC,"Resident","RemoveNullInt(!Resident!)","PYTHON_9.3")
    arcpy.CalculateField_management(FC,"Total","RemoveNullInt(!Total!)","PYTHON_9.3")


Features = arcpy.ListFeatureClasses()

for fc in Features:
    print "Formatting: " + fc
    formatFeatureClass(fc)

print "Scripting Complete"

Hope this comes in handy one day!

Brett Elliot

Leave a Reply

Your email address will not be published.