Calculate Field (Data Management)

License Level:BasicStandardAdvanced

Summary

Calculates the values of a field for a feature class, feature layer, or raster catalog.

View examples of using Calculate Field

Usage

Syntax

CalculateField_management (in_table, field, expression, {expression_type}, {code_block})
ParameterExplanationData Type
in_table

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

Mosaic Layer; Raster Catalog Layer; Raster Layer; Table View
field

The field that will be updated with the new calculation.

Field
expression

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

SQL Expression
expression_type
(Optional)

Specify the type of expression that will be used.

  • VBThe expression will be written in a standard VB format. This is the default.
  • PYTHONThe 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.3The 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.
String
code_block
(Optional)

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

String

Code Sample

CalculateField example (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: Calculate centroids

Use CalculateField to assign centroid values to new fields.

# Name: CalculateField_Centroids.py
# Description: Use CalculateField to assign centroid values to new fields

# Import system modules
import arcpy

try: 
    # 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")
except Exception as e:
    # If an error occurred, print line number and error message
    import traceback
    import sys
    tb = sys.exc_info()[2]
    print("Line {0}".format(tb.tb_lineno))
    print(e.message)
CalculateField example: Calculate ranges

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

# Name: CalculateField_Ranges.py
# Description: Use CalculateField with a codeblock to calculate values
#  based on ranges

# 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: Calculate random values

Use CalculateField to assign random values to a new field.

# Name: CalculateField_Random.py
# Description: Use CalculateField to assign random values to a new field


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

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Yes
ArcGIS for Desktop Standard: Yes
ArcGIS for Desktop Advanced: Yes
5/7/2015