HOW TO

Replace null values with zeroes in an attribute table in ArcGIS Pro

Last Published: July 27, 2023

Summary

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.

Procedure

Replace all the null values in an attribute table using the ArcPy module

  1. Open the Python window. Click the Analysis tab, click the down arrow next to Python, and click Python Window.
  2. Import the necessary module.
import arcpy
  1. Specify the path to the feature class.
path = r'C:\Users\User\Test\Misc\Test.gdb\Feature_Name'
  1. List all the fields, and create an empty array to store all the field values.
fieldObs = arcpy.ListFields(path)  
fieldNames = []
  1. Loop through the fields, and determine the count for the values.
for field in fieldObs:  
    fieldNames.append(field.name)  
del fieldObs  
fieldCount = len(fieldNames)
  1. Create a new loop to find and replace the null fields with the desired value using the UpdateCursor() function.
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)
  1. Delete the created cursor to release the lock on the feature class.
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
  1. Place the cursor at the end of the script, and press Enter twice on the keyboard to run the script.

Replace the null values in a single field using the Calculate Field function with the Python parser

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

  1. Right-click the desired field name header and select Calculate Field to open the Calculate Field window.
The Calculate Field option
  1. The Input Table section and the Field Name section are automatically selected. Ensure Python 3 is selected in the Expression Type section.
  2. In the expression box, insert the following code.
0 if !Field_Name! is None else !Field_Name!
The Calculate Field window to be configured.
  1. Click OK.

Using the Reclassify function

  1. Right-click the desired field name header and select Calculate Field to open the Calculate Field window.
  2. The Input Table section and the Field Name section are automatically selected. Ensure Python 3 is selected in the Expression Type section.
  3. In the Helpers section, click Helper Type The Filter icon, and select Function.
  4. Double-click the Reclassify function to automatically generate the expression and code block.
  5. In the expression box, change the !Field! parameter to the desired field name.
Reclass(!Field!)
  1. In the Code Block text box, enter the following code.
def Reclass(arg):
    if arg is None:
        return 0
    return arg
The Calculate Field window to be configured with the Reclassify Python script.
  1. Click the Verify The Verify button button and click OK.

Article ID:000023190

Software:
  • ArcGIS Pro 3 0
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic