Select to view content in your preferred language

Problems with field Calculator and python scripting

2095
3
Jump to solution
11-30-2014 01:51 PM
JakeKaufman1
Deactivated User

I am trying to set up a drive time and I have classified a few roads in my attribute table as follows:

A5=35MPH

A4=30MPH

A3=20MPH

So the codes A5, A4, A3 are in a field called CFCC2, My next step was to add a new field to the attribute table called speed. Currently all the values in speed are null. My question is, how can i create a python script so that I have something that will calculate the speeds of the roads based on the classification I have given them.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Add a string field, use the field calculator emulating this

"True" if !some_field! == "some_value" else ("other value" if !some_field! == "final_value" else "False")

totally untried

"35MPH" if !CFCC2! == "A5" else ("30MPH" if !CFCC2! == "A4" else ("20MPH" if !CFCC2! == "A3" else "20MPH"))

View solution in original post

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

Add a string field, use the field calculator emulating this

"True" if !some_field! == "some_value" else ("other value" if !some_field! == "final_value" else "False")

totally untried

"35MPH" if !CFCC2! == "A5" else ("30MPH" if !CFCC2! == "A4" else ("20MPH" if !CFCC2! == "A3" else "20MPH"))

0 Kudos
curtvprice
MVP Alum

Here's a way to do it using the Calculate Field tool, with a Python dictionary -- a little easier to work with if you have more than three categories....

Calculate Field

Expression:

mph(!CFCC2!)

Parser:

PYTHON

Code Block:

speed = {"A3":30, "A4":30, "A5":35}
def mph(code):
  return speed
Zeke
by
Honored Contributor

You could also set up a domain for field CFCC2 if this is in a geodatabase. Then no need for a separate Speed field. This is a more database-centric method, since having two fields essentially duplicates the same information in different display formats.