Show Navigation | Hide Navigation
You are here:
Geoprocessing tool reference > Data Management toolbox > Fields toolset > Tools

Calculate Field (Data Management)

Release 9.2
Last modified November 29, 2010
E-mail This Topic Printable Version Give Us Feedback

Print all topics in : "Tools"


Related Topics

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

Command line syntax
An overview of the Command Line window
CalculateField_management <in_table> <field> <expression> {VB | PYTHON} {code_block}

Parameter Explanation Data Type
<in_table>

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>

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
{VB | PYTHON}

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}

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

String
Data types for geoprocessing tool parameters


Command line example

CalculateField_management c:\test data\harvestable.shp Growth '[age] / [height]' VB
CalculateField_management c:\test data\harvestable.shp NEAR_X "!COUNT_X! / !SHAPE.AREA!" PYTHON #
CalculateField_management c:\test data\harvestable.shp NEAR_X x PYTHON 'x = 5'
CalculateField_management c:\test data\harvestable.shp NEAR_X x PYTHON 'import random;x = random.random()'

Scripting syntax
About getting started with writing geoprocessing scripts
CalculateField_management (in_table, field, expression, expression_type, code_block)

Parameter Explanation Data Type
in_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 (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)

Please visit the Feedback page to comment or give suggestions on ArcGIS Desktop Help.
Copyright © Environmental Systems Research Institute, Inc.