4

I want to assign numbers to a field, WScore (double), based on field values of Soils (string). They are in the same table of a raster. I'm not trying to add text to a double. What I want to do is this: Where Soils = Ds, Oo, Mu, P, or Se, WScore = 2.5.

I have little python experience so any guidance would be really helpful. Here's what I've come up with so far.

Expression: 

WScore=Reclass(!SOILS!)

def Reclass(Soils):

    x=1
    if (Soils == "Ds"):
        x=2.5
    elif (Soils == "Oo"):
        x = 2.5
    elif (Soils == "Mu"):
        x = 2.5
    elif (Soils == "P"):
        x = 2.5
    elif (Soils == "Se"):
        x = 2.5

I'm getting error 000539, which I'm guessing is because I can't concatenate 'str' and 'int' objects, though this error # covers many.

  • It's hard to say because the code block did not get formatted correctly, but is everything starting with x=1 indented one more tab? It should be. Also, this function never returns anything (e.g. return x). Finally, I don't see any concatenating going on. – phloem Jan 8 '15 at 18:48
  • if Soils in ['Ds','Oo','Mu','P','Se']: return 2.5 is a bit terser given you assign the same numerical value to these. – alphabetasoup Jan 8 '15 at 19:00
  • I think the code block is formatted correctly now, but x=1 was indented in line with the if statement on the next line. I'll remember the extra tab next time. – barnyanka Jan 8 '15 at 19:30
6

In this example, the "soils" field is text and the "WScore" field is double. The following approach should work for you:

Pre-logic Script Code:

def reclass(x):
  if x in ["Ds", "Oo", "Mu", "P", "Se"]:
    return 2.5
  else:
    return 0

reclass(!soils!)

enter image description here enter image description here

  • One more question - If I wanted the remaining values of WScore = 0 (there are a lot), is there an "all but Ds, Oo, Mu, P, Se" way to write this without listing all the other values in the Soils field? – barnyanka Jan 8 '15 at 19:44
  • All you would need to do that would be to add another line under the if statement: else: return 0. I edited the answer to include this. – Aaron Jan 8 '15 at 20:06
1

following on phloem's comment - it appears you are almost there, you just need a few more pieces:

The following assumes you have the Field Calculator parser set to Python. I've always indented one space for Field Calculator Pre-Logic Script Code following ESRI documentation which I can't seem to locate at this time; it's counter-intuitive to most all other python (indent is 4 spaces) but it works in field calculator.

out = 0.0
def Reclass(Soils):
 global out
 if (Soils == "Ds"):
  out = 2.5
 elif (Soils == "Oo"):
  out = 2.5
 elif (Soils == "Mu"):
  out = 2.5
 elif (Soils == "P"):
  out = 2.5
 elif (Soils == "Se"):
  out = 2.5
 return out

Good Luck!

  • Aaron's method (above) worked, but I'll have other calculations to do where the return values aren't all the same, so this will probably help a lot for that. Thank you! – barnyanka Jan 8 '15 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.