HOW TO

Verwenden von IF-Anweisungen im Werkzeug Feld berechnen in ArcGIS Pro

Last Published: July 5, 2024

Zusammenfassung

In ArcGIS Pro, the Calculate Field tool uses IF statements to calculate new values in the fields of an attribute table. This article highlights some common uses of IF statements using Python scripts in the Calculate Field tool in ArcGIS Pro.

Vorgehensweise

To access the Calculate Field tool, refer to ArcGIS Pro: Fundamentals of field calculations for more information.

Note: 
Indentations are automatically inserted into the Code Block box when the script is written line by line.

Populate the field based on the value of another field

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

  1. In the Calculate Field geoprocessing pane, under Parameters, select a table for Input Table.
  2. Select a field for Field Name (Existing or New).
  3. Select Python 3 for Expression Type.
  4. In the Expression text box above the Code Block box, enter the following script:
reclass(!X1!)
The Calculate Filed Geoprocessing pane to set the Parameters
  1. In the Code Block box, enter the following script:
  • Sample script with the if...else statement.
def reclass(X1):
    if (X1 == 0):
        return "N"
    else:
        return "Y"
  • Sample script with the if...elif...else statement.
def reclass(X1):
    if (X1 == 0):
        return "N"
    elif (X1 == 1 or X1 == 2):
        return "Y"
    else
        return "Invalid"
The Code Blocks box with the valid script

Classify the 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 the field according to the different ranges of values in the X1 field, with less than 101 classified as Class 1, and so forth.

  1. In the Calculate Field geoprocessing pane, under Parameters, select a table for Input Table.
  2. Select a field for Field Name (Existing or New).
  3. Select Python 3 for Expression Type.
  4. In the Expression text box above the Code Block box, enter the following script:
reclass(!X1!)
  1. In the Code Block box, enter the following script:
def reclass(X1):
    if (X1 < 101):
        return 1
    elif (X1 > 100 and X1 < 201):
        return 2
    elif (X1 > 200 and X1 < 301):
        return 3
    elif (X1 > 300):
        return 4
The Code Blocks box with the valid script

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 is a number, as shown in the example below.

  1. In the Calculate Field geoprocessing pane, under Parameters, select a table for Input Table.
  2. Select a field for Field Name (Existing or New).
  3. Select Python 3 for Expression Type.
  4. In the Expression text box above the Code Block box, enter the following script:
myCalc(!PLACE!)
  1. In the Code Block box, enter the following script:
def myCalc(num):
    if (num.isdigit()):
        return num
    else:
        return " "
The Code Blocks box with the valid script

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 certain criteria. The following script copies a value from the X1 or X2 field to a new field based on the value in the X1 field.

  1. In the Calculate Field geoprocessing pane, under Parameters, select a table for Input Table.
  2. Select a field for Field Name (Existing or New).
  3. Select Python 3 for Expression Type.
  4. In the Expression text box above the Code Block box, enter the following script:
def calc(X1, X2): 
    if X1 == 0:
        return X1
    else:
        return X2
The Code Blocks box with the valid script

Change the value of the 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 the two other fields. The following script changes the value in the ADDR:POSTC field to 'Closed' if there is a value of less than 10 in the X1 field and the value in the X2 field is 0. If it does not meet both conditions, the ADDR:POSTC value is changed to ‘open’.

def myCalc(X1,X2):
    if (X1<10)and(X2=='0'):
        return 'Closed'
    else:
        return 'open'
The Code Blocks box with the valid script

Artikel-ID: 000027512

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