Calculate Field (Data Management)

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

The input table will be modified; a copy should be made to preserve the original information.


Usage Tips

Syntax

CalculateField_management (in_table, field, expression, expression_type, code_block)
Parameter Explanation Datatype
Input Table (Required)

The table that contains the rows from a specified field that will be calculated. The updated values will be added to this table.

Table View | Raster Layer
Field Name (Required)

The field that will be updated with the new calculation.

Field
Expression (Required)

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.

  • 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.

String
Code Block (Optional)

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

String
Data types for geoprocessing tool parameters

Script Example

EXAMPLE 1: VB-based and Python-based Calculation

import arcgisscripting
gp = arcgisscripting.create()

# Set a default workspace
gp.workspace = "c:/test_data"

try:
    # Calculate field to a new value
    gp.CalculateField_management("harvestable.shp", "growth", "[age] / [height]", "VB")
    gp.CalculateField_management("harvestable.shp", "NEAR_X", "x", "PYTHON", "'import random;x = random.random()'")

except:
    # If an error occurs when running Addfield, print out the error message.
    print gp.GetMessages(2)
EXAMPLE 2:  PYTHON-based Calculation using geometry properties

# Calculate x and y centroid fields using the geometry property Centroid
import arcgisscripting, sys
gp = arcgisscripting.create()

inputFC = sys.argv[1]

gp.AddField_management(inputFC, "xCentroid", "DOUBLE", 18, 11)
gp.AddField_management(inputFC, "yCentroid", "DOUBLE", 18, 11)

# Centroid property returns a string with x and y separated by a space
xExpression = "float(!SHAPE.CENTROID!.split()[0])"
yExpression = "float(!SHAPE.CENTROID!.split()[1])"

gp.CalculateField_management(inputFC, "xCentroid", xExpression, "PYTHON")
gp.CalculateField_management(inputFC, "yCentroid", yExpression, "PYTHON")
EXAMPLE 3: PYTHON-based Calculation using codeblock parameter

# Calculate an area class based on the size of polygons
import arcgisscripting, sys
gp = arcgisscripting.create()

# An input polygon feature class
inputFC = sys.argv[1]

gp.AddField_management(inputFC, "areaclass", "short")
# Calculation is based on a custom getclass definition
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"
gp.CalculateField_management(inputFC, "areaclass", expression, "PYTHON", codeblock)

See Also

  • Add Field (Data Management)
  • Add Item (Coverage)
  • Delete Field (Data Management)
  • Drop Item (Coverage)