HOW TO
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.
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.
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.
reclass(!X1!)
def reclass(X1): if (X1 == 0): return "N" else: return "Y"
def reclass(X1): if (X1 == 0): return "N" elif (X1 == 1 or X1 == 2): return "Y" else: return "Invalid"
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.
reclass(!X1!)
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
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.
myCalc(!PLACE!)
def myCalc(num): if (num.isdigit()): return num else: return " "
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.
def calc(X1, X2): if X1 == 0: return X1 else: return X2
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'
Get help from ArcGIS experts
Download the Esri Support App