操作方法

操作方法:在字段计算器中使用 IF 语句

Last Published: May 10, 2024

摘要

The Field Calculator uses IF statements to calculate new values in the field of an attribute table. This article describes some common uses of IF statements in the Field Calculator and with Python scripts.

过程

To access the Field Calculator, refer to ArcMap: Making field calculations for more information. The following scripts use the Python parser with the Show Codeblock option checked and the expression provided.

Note:
Indentations are not automatically inserted into the Pre-Logic Script Code box. Follow the exact indentations specified in the script sample for the code to function properly.

Convert a field value based on a specified condition

Use the IF statement to convert the value of a text field based on the condition set. The following script populates the field with 'Yes' or 'No' if the value in the Valid field is '1' or '0' respectively.

def convert(Valid):
    if (Valid == '1'):
        return 'Yes'
    elif (Valid == '0'):
        return 'No'
    else:
        return 'N/A'

In the Expression text box below the Pre-Logic Script Code section, enter the following:

convert(!Valid!)
The field calculator window for example 1

Populate a field based on the value of another field

Use the IF statement to populate a field based on the value of another field. The following script populates a field with 'Y' or 'N' if the value in the ObjectID_2 field is '1' or '0' respectively.

def reclass(OBJECTID_2):
    if (OBJECTID_2 == 0):
        return "N"
    else:
        return "Y"

In the Expression text box below the Pre-Logic Script Code section, enter the following:

reclass(!OBJECTID_2!)
The expression to populate a field with Y or N based on the OBJECTID_2 field the Field Calculator

Classify a field according to a range of values from another field

A field can be populated with different values according to a range of values from another field. The following script classifies a field according to the different ranges of elevation in the Top_Elevat field, with less than 8,000 ft. classified as Class 1, and so forth.

def reclass(TOP_ELEVAT):
    if (TOP_ELEVAT < 8000):
        return 1
    elif (TOP_ELEVAT > 8000 and TOP_ELEVAT < 9001):
        return 2
    elif (TOP_ELEVAT > 9000 and TOP_ELEVAT < 10001):
        return 3
    elif (TOP_ELEVAT > 10000):
        return 4

In the Expression text box below the Pre-Logic Script Code section, enter the following:

reclass(!TOP_ELEVAT!)
The expression that classifies a field according to the different ranges of elevation in the Top_Elevat field in the Field Calculator

Copy values from one field to another if the value contains a number

Use the .isdigit() function to copy values from one text data type field to another only if the value begins with a number, as shown in the example below.

def myCalc(num):
    if (num[0].isdigit()):
        return num
    else:
        return " "

In the Expression text box below the Pre-Logic Script Code section, enter the following:

myCalc(!<fieldnamehere>!)
The expression to copy values from one text data type field to another if the value begins with a number in the Field Calculator

Copy values from one of two fields to another field

An IF statement can be used to copy the values from one of two fields that meets a certain criteria. The following script copies a value from the Acres or Legal field to a new field based on the value in the Legal field.

def calc(legal, acres):
    if legal == 0:
        return acres
    else:
        return legal

In the Expression text box below the Pre-Logic Script Code section, enter the following:

calc(!Legal!, !Acres!)
The expression to copy a value from the Acres or Legal field to a new field based on the value in the Legal field in the Field Calculator

Change the value of a field if it meets the criteria of two fields

An IF statement can be used to change the value of one field that meets the condition of two other fields. The following script changes the value in the ActivityStatus field to 'Closed', if there is a value of less than -10 in the CountDown field and has an 'Approved' value in the ActivityStatus field. If it does not meet both conditions, the ActivityStatus field is left as it is.

def myCalc(status,activity):
    if (status<-10)and(activity=='Approved'):
        return 'Closed'
    else:
        return activity

In the Expression text box below the Pre-Logic Script Code section, enter the following:

myCalc(!CountDown!, !ActivityStatus!)
The expression to change the value in the ActivityStatus field if there is a value of less than -10 in the CountDown field in the Field Calculator

文章 ID: 000022370

接收通知并查找新问题或常见问题的解决方案

从我们全新的 AI 聊天机器人中获得简明答案和视频解决方案。

下载 Esri 支持应用程序

相关信息

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

获取来自 ArcGIS 专家的帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项