Calculate Field

Title  Calculate Field

Summary

Geoprocessing tool used to perform field calculations.


Usage


Syntax

Parameter Explanation
in_table

The table containing the field that will be updated with the new calculation.

field

The field that will be updated with the new calculation.

expression

The simple calculation expression used to create a value that will populate the selected rows.

expression_type (Optional)

Specifies the type of expression that will be used.

  • VB—The expression will be written in a standard VB format. This is the default.
  • PYTHON—The expression will be written in a standard Python format. Use of geoprocessor methods and properties is the same as creating a 9.2 version geoprocessor.
  • PYTHON_9.3—The expression will be written in a standard Python format. Use of geoprocessor methods and properties is the same as creating a 9.3 version geoprocessor.

Field calculations with a VB Expression type are not supported on 64-bit products, including ArcGIS Pro, ArcGIS Desktop—Background Geoprocessing (64-bit)—and ArcGIS Server. To successfully use Calculate Field in these products, expressions should be converted to Python, or in the case of Background Geoprocessing (64-bit), background processing can alternatively be disabled.

code_block (Optional)

Allows for a block of code to be entered for complex expressions.

Code Samples

CalculateField example 1 (Python window)

The following Python window script demonstrates how to use the CalculateField function in immediate mode.


import arcpy
arcpy.env.workspace = "C:/data"
arcpy.AddField_management("vegtable.dbf", "VEG_TYP2", "TEXT", "", "", "20")
arcpy.CalculateField_management("vegtable.dbf", "VEG_TYP2", 
                                '!VEG_TYPE!.split(" ")[-1]', "PYTHON_9.3")
                    

CalculateField example 2 (stand-alone script)

Use CalculateField to assign centroid values to new fields.


# Name: CalculateField_centroids.py

# Import system modules
import arcpy

# Set environment settings
arcpy.env.workspace = "C:/data/airport.gdb"
 
# Set local variables
inFeatures = "parcels"
fieldName1 = "xCentroid"
fieldName2 = "yCentroid"
fieldPrecision = 18
fieldScale = 11
 
# Add fields
arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", 
                          fieldPrecision, fieldScale)
arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE", 
                          fieldPrecision, fieldScale)
 
# Calculate centroid
arcpy.CalculateField_management(inFeatures, fieldName1, 
                                "!SHAPE.CENTROID.X!",
                                "PYTHON_9.3")
arcpy.CalculateField_management(inFeatures, fieldName2, 
                                "!SHAPE.CENTROID.Y!",
                                "PYTHON_9.3")

                    

CalculateField example 3 (stand-alone script)

Use CalculateField with a code block to calculate values based on ranges.


# Name: CalculateField_ranges.py

# Import system modules
import arcpy
 
# Set environment settings
arcpy.env.workspace = "C:/data/airport.gdb"
 
# Set local variables
inTable = "parcels"
fieldName = "areaclass"
expression = "getClass(float(!SHAPE.area!))"

codeblock = """
def getClass(area):
    if area <= 1000:
        return 1
    if area > 1000 and area <= 10000:
        return 2
    else:
        return 3"""
 
# Execute AddField
arcpy.AddField_management(inTable, fieldName, "SHORT")
 
# Execute CalculateField 
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON_9.3", 
                                codeblock)
                    

CalculateField example 4 (stand-alone script)

Use CalculateField to assign random values to a new field.


# Name: CalculateField_Random.py

# Import system modules
import arcpy
import random
 
# Set environment settings
arcpy.env.workspace = "C:/data/airport.gdb"
  
# Set local variables
inFeatures = "parcels"
fieldName = "RndValue"
expression = "random.randint(0, 10)"
code_block = "import random"
 
# Execute AddField
arcpy.AddField_management(inFeatures, fieldName, "LONG")
 
# Execute CalculateField 
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON_9.3", 
                                code_block)

                    

Tags

Credits

Use limitations