HOW TO

Replace null values with zeroes in an attribute table in ArcMap

Last Published: May 26, 2020

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).

Procedure

This article describes two ways to replace null values in an attribute table; by replacing the null values in the attribute table, or by replacing the null values in a single column (field).

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

Note:
The following script can be used in the Python console of ArcMap, or as a stand-alone script.
  1. Import the necessary module.
import arcpy
  1. Specify the path to the feature class.
path = r'C:\Users\User\Test\Misc\Test.gdb\Testing'
  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

The following shows the full script:

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

Replace the null values in a single field using the Field Calculator and Python parser

  1. In ArcMap, click the Editor drop-down menu on the Editor toolbar, and select Start Editing.
  2. In the Table Of Contents, right-click the selected layer, and select Open Attribute Table.
An image of a field with null values in an attribute table.
  1. Right-click the field with null values, and select Field Calculator.
An image of selecting the field calculator of a field.
  1. In Field Calculator, select the Python parser, and check the Show Codeblock check box.
  2. In the Pre-Logic Script Code box, copy and paste the following code:
def updateValue(value):
  if value == None:
   return '0'
  else: return value
  1. Type the following code in the second box, and replace '!Field_Name!' with the field name from the Fields list, as shown in the image below.
updateValue(!Field_Name!)

An image of calculating null values in the Field Calculator.
  1. Click OK, and the null values are replaced with zeros in the field, as shown in the image below.
An image of null field values replaced with zero.
  1. On the Editor toolbar, click Stop Editing and Save Edits to keep the changes.

Article ID:000016100

Software:
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options