Basic Field Calculations for ArcGIS®

license mit contributions welcome

Get started

Tabular is an unofficial, interactive manual for the Calculate Field tool in ArcGIS Desktop. Tabular is designed for new GIS and Python users to learn scripting for spatial analysis.

You can copy-and-paste Python expressions into ArcGIS, navigate through the tool with screenshots, and test your code in Python sandboxes. Or you can download the cheat sheet.

Edit some text


Find characters

Goal Code snippet------------------------------------------------------------------
Return the first character
!fieldname![0]
Return the first through third characters
!fieldname![0:3]
Return the second-to-last character
!fieldname![-2]

Remove characters

Goal Code snippet
Remove all whitespace
!fieldname!.rstrip()
Swap certain words
!fieldname!.replace("value", "New Value")
Remove all non-alphanumeric characters
‘’.join(ch for ch in !fieldname! if ch.isalnum())
Return <Null>
"None"

Format the string

Goal Code snippet-----------------------------------------------
Capitalize the string
!fieldname!.capitalize()
Convert the string to lowercase
!fieldname!.lower()
Convert the string to all caps
!fieldname!.upper()

Do some math


Add and subtract

Goal Code snippet--------------------------------------------
Add 5 to the field value
!fieldname! + 5
Subtract 6 from the field value
!fieldname! - 6

Multiply and divide

Goal-------------------------------------- Code snippet--------------------------------------------
Multiply by 2
!fieldname! * 2
Divide by 3
!fieldname! / 3

Execute simple functions

In the Field Calculator, you can execute anything listed under Functions: or any built-in Python functions.

Goal Code snippet---------------------------------
Calculate the logarithm
math.log(!fieldname!)
Round to 2 decimal places
round(!fieldname!, 2)

Deal with geometries


Find feature extents

Goal Code snippet--------------------------------------------
Find the most extreme longitude
!shape.extent.XMax!
Find the most extreme latitude
!shape.extent.YMax!

Calculate line lengths

Goal Code snippet----------------------------------------------------------
Calculate feature length in map units
!shape.length!
Calculate feature length in feet
!shape.length@feet!
Calculate feature length in geodesic feet
!shape.length@Geodesicfeet!

Compute shape areas

Goal Code snippet---------------------------------------------------------------------
Calculate feature area in map units
!shape.area!
Calculate feature area in square feet
!shape.area@squarefeet!
Calculate feature area in geodesic square feet
!shape.area@Geodesicsquarefeet!

Further reading

You can perform more advanced calculations with the code block in the Field Calculator. Check out some examples in the official Esri documentation.

Shitij Mehta wrote a blog series on if-then-else statements that you can apply to field calculations.

David Verbyla made a video on Python functions for the Field Calculator.