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

For a comparison of 9.2 and 9.3 geoprocessor, see Creating the geoprocessor object.

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):\n\
    if area <= 1000:\n\
        return 1\n\
    if area > 1000 and area <= 10000:\n\
        return 2\n\
    else:\n\
        return 3"
gp.CalculateField_management(inputFC, "areaclass", expression, "PYTHON", codeblock)
EXAMPLE 4: PYTHON-based Calculation using the arcgis.rand function

# Calculate a field to have random values derived from a uniform distribution of integers between 0 and 10
import arcgisscripting, sys
gp = arcgisscripting.create()

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

gp.AddField_management(inputFC, "Value", "LONG")
gp.CalculateField_management(inputFC, "Value", "arcgis.rand('Integer 0 10')", "PYTHON", "#")

See Also

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