Calculate Field

Title  Calculate Field

Summary

Geoprocessing tool that calculates the values of a field for a feature class, feature layer, or raster.


Usage


Syntax

Parameter Explanation
in_table

The table containing the field that will be updated with the new calculation.

field

The field that will be updated with the new calculation.If a field with the specified name does not exist in the input table, it will be added.

expression

The simple calculation expression used to create a value that will populate the selected rows.

expression_type (Optional)

Specifies the type of expression that will be used.PYTHON and PYTHON_9.3 are still supported for backward compatibility but are not listed as choices. Python scripts that use these keywords will continue to work.Python 3 (PYTHON3 in Python) returns the values in date fields as Python datetime objects. PYTHON and PYTHON_9.3 return the values in date fields as strings.Python 3—Python expression type. This is the default.Arcade—Arcade expression type.SQL—SQL expression.To learn more about Python expressions, see Calculate Field examples.To learn more about Arcade expressions, see the ArcGIS Arcade guide.To learn more about SQL expressions, see Calculate field values.SQL expressions were implemented to support faster calculations for feature services. Instead of performing calculations one feature or row at a time, a single request is set to the feature service, resulting in significantly faster calculations.Only feature services support SQL expressions. For other formats, use Python or Arcade expressions.

code_block (Optional)

A block of code that will be entered for complex expressions.

out_table (Optional)

field_type (Optional)

The field type of the new field. This parameter is only used when the field name does not exist in the input table.If the field is of type text, the new field will have a length of 512. For shapefiles and dBASE files, the field will have a length of 254. The length of the new field can be adjusted using the Alter Field tool.Text—Any string of characters. Float (single precision)— Fractional numbers between -3.4E38 and 1.2E38.Double (double precision)— Fractional numbers between -2.2E308 and 1.8E308.Short (small integer)— Whole numbers between -32,768 and 32,767.Long (large integer)— Whole numbers between -2,147,483,648 and 2,147,483,647.Date—Date and time. Blob (binary data)—Long sequence of binary numbers. Raster imagery—Raster images. All ArcGIS software-supported raster dataset formats can be stored, but it is highly recommended that only small images be used.GUID (globally unique identifier)—Globally unique identifier.

esri_out_feature_service_name (Optional)

The name of the optional feature service to create on the federated server containing the result of this tool. If no name is specified an output feature service will not be created.

Code Samples

CalculateField example 1 (Python window)

The following Python window script demonstrates how to use the CalculateField function in immediate mode.


import arcpy
arcpy.env.workspace = "C:/data"
arcpy.AddField_management("vegtable.dbf", "VEG_TYP2", "TEXT", "", "", "20")
arcpy.CalculateField_management("vegtable.dbf", "VEG_TYP2", 
                                '!VEG_TYPE!.split(" ")[-1]', "PYTHON3")

                    

CalculateField example 2 (stand-alone script)

Use CalculateField to assign centroid values to new fields.


# Name: CalculateField_centroids.py

# Import system modules
import arcpy

# Set environment settings
arcpy.env.workspace = "C:/data/airport.gdb"
 
# Set local variables
inFeatures = "parcels"
fieldName1 = "xCentroid"
fieldName2 = "yCentroid"
fieldPrecision = 18
fieldScale = 11
 
# Add fields
arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", 
                          fieldPrecision, fieldScale)
arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE", 
                          fieldPrecision, fieldScale)
 
# Calculate centroid
arcpy.CalculateField_management(inFeatures, fieldName1, 
                                "!SHAPE.CENTROID.X!",
                                "PYTHON3")
arcpy.CalculateField_management(inFeatures, fieldName2, 
                                "!SHAPE.CENTROID.Y!",
                                "PYTHON3")

                    

CalculateField example 3 (stand-alone script)

Use CalculateField with a code block to calculate values based on ranges.


# Name: CalculateField_ranges.py

# Import system modules
import arcpy
 
# Set environment settings
arcpy.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, "PYTHON3", 
                                codeblock)

                    

CalculateField example 4 (stand-alone script)

Use CalculateField to assign random values to a new field.


# Name: CalculateField_Random.py

# Import system modules
import arcpy
import random
 
# Set environment settings
arcpy.env.workspace = "C:/data/airport.gdb"
  
# Set local variables
inFeatures = "parcels"
fieldName = "RndValue"
expression = "random.randint(0, 10)"
code_block = "import random"
 
# Execute AddField
arcpy.AddField_management(inFeatures, fieldName, "LONG")
 
# Execute CalculateField 
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON3", 
                                code_block)

                    

CalculateField example 5 (Python window)

The following Python window script demonstrates how to use the CalculateField function with ARCADE in immediate mode.


import arcpy
arcpy.env.workspace = "C:/data/fgdb.gdb"
arcpy.CalculateField_management("data", "new_value", "$feature.value1 + $feature.value2", "ARCADE")

                    

CalculateField example 6 (Python window)

The following Python window script demonstrates how to use the CalculateField function with SQL in immediate mode.


import arcpy
feature_service = "<a feature service url>"
arcpy.CalculateField_management("data", "NEW_VALUE", "SAMPLE * (BASELINE - 40)", "SQL")

                    

Tags

Credits

Use limitations