Common Field Calculations in ArcGIS
The ArcGIS field calculator can save a person time and allow for fairly quick data cleanup IF you can remember how to use it. I don’t know about you, but for me the layout of the field calculator, the separation of the formula field and the code block area and the minor changes from standard Python formatting are enough to send me straight to the Google search bar if I haven’t used it in a while. That’s why I started making notes for even the simplest of tasks. I go back to them often saving myself time and frustration.
Maybe someday I’ll memorize these, but for now this works.
Below you’ll find a list of some simple field calculations that I use often as a starting place. I’ll add to these as I think of them. Maybe you’ll find one useful.
Common Field Calculations using Python:
Change text to title case - So simple and used frequently.
# expression: !note1!.title()
Replace one character for another - Probably the most common reason I use the field calculator.
# expression: !noteField!.replace("-", "_")
Concatenate fields together - Very common when geocoding addresses.
# expression: !Address! + " " + !Street! + ", " + !City! + ", " + !State! + " " + !Zip!
Simple mathematic calculations across multiple fields - You can use the field calculator to extract summary statistics from a list of fields.
# find the minimum value across a list of fields: # expression: min([!field1!, !field2!, !field3!]) # find the maximum value across a list of fields: # expression: max([!field1!, !field2!, !field3!]) # find the sum of all values from a list of fields: # expression: sum([!field1!, !field2!, !field3!]) # find the average value from a list of fields: # expression: sum([!field1!, !field2!, !field3!]) / len([!field1!, !field2!, !field3!])
Chaining commands together In this example I start with a field called TextField with some messy data. Suppose I have Oak Trees listed as oak, Oak, Oak tree, and oak tree but I want to normalize them all to read simply Oak Tree. A few simple commands can be chained together to accomplish this.
# expression: !TextField!.lower().replace("tree", " ").replace("oak", "Oak Tree")
Using the Code Block The Code Block portion of the Calculate Field tool in ArcGIS allows for the use of any Python function, you can even import modules and define your own functions. The format is not that complicated once you’ve used it a few times. Think of the Code Block area as a place to define functions which you can later call from the Expression box. In the following example I want to create an abbreviated code for each tree species by taking the first two letters of the genus and concatenating them together with the first two letters of the species. I can accomplish that quickly like this:
# expression: abbreviate(!SciName!) # code block: def abbreviate(x): words = x.split() letters = [word[:2] for word in words] return "".join(letters)
Unit conversion using math To convert from square footage to acres and round to two decimal places you could do something like this:You could accomplish the same result with a single line as below, but I personally prefer to use the Code Block to make things more repeatable and to allow for more complex functions in the future:
# expression: sqft_to_acres(!Shape_Area!) # code block: def sqft_to_acres(area): return round((area / 43560),2)
round((!Shape_Area! / 43560),2)
Another way to convert units - As detailed here, it’s possible to use the ArcGIS Pro built in geometry unit conversions rather than doing your own calculations. Have a look at the esri link above to see all of the built in unit of measurement conversions. Note that the format of field names is slightly different when referring to geometry columns as compared to other fields:
# Current area to hectares # expression: !shape.area@hectares! # Current length to kilometers # expression: !shape.length@KILOMETERS!
Using logic - Here’s an overly simplified example using logic to classify a utility pipe as either a Main line or a Lateral line based on pipe diameter.
# expression: lat_main(!size!) # code block: def lat_main(diameter): if (diameter >= 8): return "Main" else: return "Lateral"