News

ArcWatch: Your e-Magazine for GIS News, Views, and Insights

February 2011


Combine Several Fields into One Using a Single Line of Python Code

By Barbara Bicking
Esri Software Products

An in-depth knowledge of Python isn't necessary to benefit from this powerful scripting language when working with ArcGIS Desktop ArcToolbox tools like Calculate Field. Knowing just a few pieces of Python code can make the difference between struggling with and easily completing a geoprocessing workflow.

For example, a single line of Python code can help you remove a space to form a valid output name, append a unique identifier to an output name, or combine two or three fields and their values in a large attribute table for easy viewing and analyzing.

This works well if you are working with customer and product attribute tables. For instance, for the purposes of production planning and distribution scheduling and routing, you want to group your client base by business type, sales volume, and location using the ZIP Code. You can combine these fields and their values using the ArcToolbox Calculate Field tool with a one-line Python expression. In the following example, the field TypSalesZip is added to your customer table; its values are a combination of the values of the business TYPE field, the sales from the SALES field, and the location from the ZIP field to form a string like this: Store_160910_30312.

The steps below set up the workflow that gives you that result in ModelBuilder.

Step 1: From ArcMap, create a model with ModelBuilder.

Step 2: To ensure that your source data stays intact, open ArcToolbox and drag the Copy Rows tool, located in the Data Management toolbox in the Table toolset, into ModelBuilder to copy the data. Give the output a meaningful, descriptive name.

Step 3: Drag the Add Field tool, located in the Data Management toolbox in the Fields toolset, into ModelBuilder; connect it to the Copy Rows output; and set its Field Name and Field Type parameters. The example here uses TypSaleZip and TEXT , respectively.


The Field Value Formatting Workflow in ModelBuilder

Step 4: Drag the Calculate Field tool, located in the Data Management toolbox in the Fields toolset, into ModelBuilder and connect it to Add Field's output. Open the Calculate Field dialog box and enter the following Python expression:

!TYPE!.replace(" ", "") + "_" + str(int(!SALES!)) + "_" + !ZIP!

To access a field's value and format it, here with the replace (, ) function, Python brackets the field name with exclamation marks, like this:

!<fieldname>!.

The first part of the expression—!TYPE!.replace(" ", "") up to the plus sign—takes the string of the source field and removes any spaces. The second part of the expression—str(int(!SALES!))—first turns the decimal dollar amount for SALES into an integer using int(), then it turns the integer dollar value into a string using str() and appends it to the business type with an underscore. The third and final piece of the expression—!ZIP!—appends the ZIP Code with an underscore. Here's a section of the result.


Section of the Field Value Formatting Model's Result

You can use this Python snippet to format field values in other workflows, too.

 
Contact Us | Privacy | Legal | Site Map