# Common Field Calculations using Python in ArcGIS

+" "+ !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.