HOW TO
An attribute table may contain multiple fields with null values and by default, these fields are populated with an empty space. In some cases, some tools or functions do not execute if the fields are not populated with characters (non-nullable fields). This article provides two possible methods to replace null values in an attribute table.
import arcpy
path = r'C:\Users\User\Test\Misc\Test.gdb\Feature_Name'
fieldObs = arcpy.ListFields(path) fieldNames = []
for field in fieldObs: fieldNames.append(field.name) del fieldObs fieldCount = len(fieldNames)
with arcpy.da.UpdateCursor(path, fieldNames) as curU: for row in curU: rowU = row for field in range(fieldCount): if rowU[field] == None: rowU[field] = "0" curU.updateRow(rowU)
del curU
This is the full script used in this article.
import arcpy path = r'C:\Users\User\Desktop\Misc\Piracy.gdb\Continents' fieldObs = arcpy.ListFields(path) fieldNames = [] for field in fieldObs: fieldNames.append(field.name) del fieldObs fieldCount = len(fieldNames) with arcpy.da.UpdateCursor(path, fieldNames) as curU: for row in curU: rowU = row for field in range(fieldCount): if rowU[field] == None: rowU[field] = "0" curU.updateRow(rowU) del curU
There are two options to replace Null values in a single field. Use the conditional operator in the Python parser, or if 3D Analyst is licensed, use the Reclassify function.
Using the conditional operator
0 if !Field_Name! is None else !Field_Name!
Using the Reclassify function
Reclass(!Field!)
def Reclass(arg): if arg is None: return 0 return arg
Get help from ArcGIS experts
Download the Esri Support App