Python Calculate Field If Statement

10471
14
02-14-2011 10:57 AM
Ryan_Galbraith
New Contributor III
Just wondering if the Python-Geoprocessing Community could help me.  I am trying to write the following code block into a calculate field called weight from the values in another field called Symbol.

def Reclass( !Weight!):
  if ( !SYMBOL! == 6):
    return 1.2
  elif ( !SYMBOL! == 85):
    return 1.5
  elif ( !SYMBOL! ==2):
    return 1
  elif ( !SYMBOL! ==42):
    return 1.8
  elif ( !SYMBOL! ==58):
    return 2

It just won't work.  help...
0 Kudos
14 Replies
by Anonymous User
Not applicable
You're gonna have to give more info than that. And no, that method won't work.
0 Kudos
DanPatterson_Retired
MVP Emeritus
enclose your code in code blocks (# in the html editor window) so that indentation can be checked. Include a screen grab so the whole field calculator dialog window can be checked for missing elements (like how you pass the field name to the function and to make sure that the Python radiobutton is checked on)
also any further details on the field's properties (perhaps a wrong field type, precision or scale)
0 Kudos
by Anonymous User
Not applicable
Original User: rdg129

You're gonna have to give more info than that. And no, that method won't work.


This was what I was trying, but really lost track of what to put where.
0 Kudos
ChrisSnyder
Regular Contributor III
Just a suggestion, but if this is part of a Python script, you are WAY better off using an updatecursor instead. The benefit is readability, codeability, and debugging. The code block for the FieldCalculator is notoriously hard to write code for, while the updatecursor has very clear and clean syntax: http://forums.esri.com/Thread.asp?c=93&f=1729&t=262236#806786
0 Kudos
by Anonymous User
Not applicable
Original User: niklas.norrthon

This goes in the code block:
symbol_weights = {
    6: 1.2,
    85: 1.5,
    2: 1.0,
    42: 1.8,
    58: 2.0,
    }   


And in the expression field you put:
symbol_weights[!SYMBOL!]
0 Kudos
NiklasNorrthon
Occasional Contributor III
Just a suggestion, but if this is part of a Python script, you are WAY better off using an updatecursor instead. The benefit is readability, codeability, and debugging. The code block for the FieldCalculator is notoriously hard to write code for, while the updatecursor has very clear and clean syntax


I disagree. Calculate field with short snippets of python code as in this case are perfect in most situations. The one place to avoid this tool and use an update cursor instead is within a python script, becuase of the quoting issues.

But as a stand alone tool, or in a model builder model, CalculateField is a very useful tool.
by Anonymous User
Not applicable
Original User: csny490

If you are going the Python route, I still assert that a cursor is better because:
1. It's faster and easier to write the code (no need to post to the forums per the pain in the ass \\n\\n\\n shinanigans).
2. When you print the code it's actually understandable by a human, where as the codeblock generally comes out in one big line and is generally unintelligible.

For example (yes this is VB, but you get my drift):

KLAScodeblock = "If [CMPAREA] <= 1000 Then\\nklasse = 100\\n\\nElseif [MIN] <= "+MDV+"  Then \\nklasse = 1\\n\\nElseif [MIN] <= "+MDA+" Then \\nklasse = 2\\n\\nElse \\nklasse = 3\\n\\nEnd  If\\n\\n\\n"
0 Kudos
Ryan_Galbraith
New Contributor III
This goes in the code block:
symbol_weights = {
    6: 1.2,
    85: 1.5,
    2: 1.0,
    42: 1.8,
    58: 2.0,
    }   


And in the expression field you put:
symbol_weights[!SYMBOL!]


It looks good, but I am still getting an error.

Here is what I put in.

ERROR

000539: Error running expression: SYMBOL_weights[2] <type 'exceptions.NameError'>: name 'SYMBOL_weights' is not defined


Failed to execute (Calculate Field).


Failed at Wed Feb 16 13:17:20 2011 (Elapsed Time: 1.00 seconds)
0 Kudos
DanPatterson_Retired
MVP Emeritus
let us examine how Python is used in the field calculator as indicated from the help files....

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005s0000002m000000.htm

There is an Expression box, and a Code block, in the code block there is a section for variables and/or imports to be denoted (in this case an import statement) and a  Python function, as denoted by the def
If the syntax is correct and the fieldname is enclosed in double !'s then it should work
Parser:
Python

Expression:
getRandomValue()

Code Block:
import numpy.random as R

def getRandomValue():
    return R.random()



can you get this to work in a field?  The error in the previous example is that SYMBOL_weights is not a function
0 Kudos