Common Field Calculations using Python in ArcGIS | by Paul Bartsch | Towards Data Science

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.
Change text to title case. — So simple and used frequently.
!note1!.title()
Replace one character for another. — Probably the most common reason I use the field calculator.
!noteField!.replace("-","_")
Concatenate fields together. — Very common when geocoding addresses.
!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.
Chain 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.
!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:
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:
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:
!shape.area@hectares!
!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.
Originally published at geopy.dev