Calculate Field (Data Management)

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
from arcpy import env
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")
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
from arcpy import env

try: 
    # Set environment settings
    env.workspace = "C:/data/airport.gdb"
 
    # Set local variables
    inFeatures = "parcels"
    fieldName1 = "xCentroid"
    fieldName2 = "yCentroid"
    fieldPrecision = 18
    fieldScale = 11
    # Expressions are calculated using the Shape Field's geometry property
    expression1 = "float(!SHAPE.CENTROID!.split()[0])"
    expression2 = "float(!SHAPE.CENTROID!.split()[1])"
 
    # Execute AddField
    arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", 
                              fieldPrecision, fieldScale)
    arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE", 
                              fieldPrecision, fieldScale)
 
    # Execute CalculateField 
    arcpy.CalculateField_management(inFeatures, fieldName1, expression1,
                                    "PYTHON")
    arcpy.CalculateField_management(inFeatures, fieldName2, expression2,
                                    "PYTHON")
except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

CalculateField example: Calculate ranges

Use CalculateField with a codeblock 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
from arcpy import env
 
# Set environment settings
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", 
                                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
from arcpy import env
 
# Set environment settings
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")

Environments

Related Topics

Licensing Information

ArcView: Yes
ArcEditor: Yes
ArcInfo: Yes

10/27/2014