操作方法

操作方法:在 ArcGIS Pro 中将属性表中的空值替换为零

Last Published: July 27, 2023

摘要

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

过程

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.

文章 ID:000023190

从 ArcGIS 专家处获得帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项

相关信息

发现关于本主题的更多内容