HOW TO

Ersetzen von NULL-Werten durch Nullen in einer Attributtabelle in ArcGIS Pro

Last Published: July 27, 2023

Zusammenfassung

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

Vorgehensweise

The following are two possible methods to replace null values in an attribute table.

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.

Artikel-ID: 000023190

Benachrichtigungen erhalten und Lösungen für neue oder häufige Probleme finden

Unser neuer KI-Chatbot stellt zusammengefasste Antworten und Videolösungen für Sie bereit.

Esri Support App herunterladen

Zugehörige Informationen

Weitere Informationen zu diesem Thema erkunden

Unterstützung durch ArcGIS-Experten anfordern

An den technischen Support wenden

Esri Support App herunterladen

Zu Download-Optionen wechseln