操作方法:在 ArcGIS pro 中将属性表中的空值替换为零 |
您所在的位置:网站首页 › 如何把表格中的零设为空 › 操作方法:在 ArcGIS pro 中将属性表中的空值替换为零 |
摘要
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 Open the Python window. Click the Analysis tab, click the down arrow next to Python, and click Python Window.Import the necessary module. import arcpy Specify the path to the feature class. path = r'C:\Users\User\Test\Misc\Test.gdb\Feature_Name' List all the fields, and create an empty array to store all the field values. fieldObs = arcpy.ListFields(path) fieldNames = [] Loop through the fields, and determine the count for the values. for field in fieldObs: fieldNames.append(field.name) del fieldObs fieldCount = len(fieldNames) 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) Delete the created cursor to release the lock on the feature class. del curUThis 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 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: Right-click the desired field name header and select Calculate Field to open the Calculate Field window. The Input Table section and the Field Name section are automatically selected. Ensure Python 3 is selected in the Expression Type section.In the expression box, insert the following code. 0 if !Field_Name! is None else !Field_Name! Click OK.Using the Reclassify function: Right-click the desired field name header and select Calculate Field to open the Calculate Field window.The Input Table section and the Field Name section are automatically selected. Ensure Python 3 is selected in the Expression Type section.In the Helpers section, click Helper Type , and select Function.Double-click the Reclassify function to automatically generate the expression and code block.In the expression box, change the !Field! parameter to the desired field name. Reclass(!Field!) In the Code Block text box, enter the following code. def Reclass(arg): if arg is None: return 0 return arg Click the Verify button and click OK.文章 ID:000023190 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |