The input table will be modified; a copy should be made to preserve the original information.
Calculate Field computes and assigns a value to the specified field of the Input table.
Expressions can be created in a standard Visual Basic (VB) format or in a standard Python format. The formatting style of the string used for the expression should be appropriate to the environment (type).
Python expressions can be created using properties from the geometry object (Type, Extent, Centroid, FirstPoint, LastPoint, Area, Length, IsMultipart, and PartCount).
In the tool's dialog, an expression can be entered directly into the Expression parameter, or interactively built using the Field Calculator.
The calculation can only be applied to one field per operation.
When calculating new values for a field, existing values will be overwritten. Retain a copy of the input table before using Calculate Field in case an error is made.
Fields are always enclosed in square brackets [ ] for VB.
Fields are always enclosed in exclamation points ! ! for Python.
To calculate strings to text or character fields, the string must be double-quoted (for example, "string") in the dialog box, whereas at the command line and in scripting, the double-quoted string must also be encapsulated in single quotes (for example, '"string"').
Calculate Field can also be used to update character items. Expressions using character string should be wrapped using single quotes, for example: [CHARITEM] = 'NEW STRING'. However, if the character string has embedded single quotes, wrap the string using double quotes. For example: [CHARITEM] = "TYPE'A'".
To calculate a field to be a numeric value, enter the numeric value in the Expression parameter; no quotes around the value are required.
The expression and code block are connected. The code block must relate back to the expression; the result of the code block should be passed into the expression. For example:
CalculateField_management C:\WUTemp\polygon_Copy.shp NEAR_X x PYTHON 'x = 5'The expression is 'x', and the code block is 'x = 5'.
The Code Block parameter allows you to create complex expressions. You can enter the code block directly on the dialog, or as a continuous string at the command line or in scripting.
The Python math modules and formatting are available for use in the Code Block parameter. Additional modules can be imported. The math module provides number-theoretic and representation functions, power and logarithmic functions, trigonometric functions, angular conversion functions, hyberbolic functions and mathematical constants. To learn more about the math module, see Python's help.
When calculating joined data, you cannot calculate the joined columns directly. However, you can directly calculate the columns of the origin table. To calculate the joined data, you must first add the joined tables or layers to ArcMap. You can then perform calculations on this data separately. These changes will be reflected in the joined columns.
In a stand-alone script, calculations using Expression Type's PYTHON option must use the arcgisscripting module. Stand-alone scripts that use win32com.client module to create the geoprocessor will fail with a "Python expression is blocked when using a dispatch based application" error.
The expression type must be Python when running Calculate Field with ArcGIS Engine or ArcGIS Server. Visual Basic for Applications (VBA) is required for many types of VB expressions and VBA is only available when Calculate Field is run by ArcGIS Desktop. Only use Python as the expression type whenever the tool is included in a model that will be published to an ArcGIS Server.
The arcgis.rand() function is supported by the CalculateField tool. The arcgis.rand() function has been created for ArcGIS tools and should not be confused with the Python Rand() function. The syntax for the available distributions for the arcgis.rand() function can be found at The distribution syntax for random values.
The following environment affects this tool: workspace.
|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.
The simple calculation expression used to create a value that will populate the selected rows.
|Expression Type (Optional)||
Specify the type of expression that will be used.
|Code Block (Optional)||
Allows for a block of code to be entered for complex expressions.
EXAMPLE 1: VB-based and Python-based Calculationimport 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 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())" yExpression = "float(!SHAPE.CENTROID!.split())" 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 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)