# Calculate Field, Helpers, Reclassify, If/elif

3129
8
10-08-2021 12:36 PM
by
New Contributor III

Hello, in PRO I am trying to use Python / Code Block to use existing field !prim_lith! (text field, with geologic material descriptions, eg, clay, sand) to populate a new field !prim_lith_reclass! (short integer field) whose value 1, 2, 3,...9 depends on the geologic material in !prim_lith!.  The Expression is valid and runs but the return values are all equal to 1 (from line 3 in the code block), see lower right of attached jpg. I've tried varied syntax etc. but nothing seems to work.  Should text be in double quotes "text", or double apostrophe 'text' ?  What is the difference between !=  and == ?  Do I have to explicitly enter an Else?

Any insights would be greatly appreciated. Thank you!

3 Solutions

Accepted Solutions
MVP Esteemed Contributor

!= means "not equal to"

== means "equal to"

I think you have put != instead of == ?

Think Location
MVP Frequent Contributor

please format your code, you're also not showing the entire code block.  It makes it very tedious to try and help you Code formatting ... the Community Version - Esri Community

Can't even see line 42 of your code.

I would add an else at the end to take into account misspellings etc. set it as something like 999,-999 or whatever.

else:
a = 999
return a

MVP Esteemed Contributor

Putting the whole codeblock here would help.

Ensure that you have returned variable a with appropriate indentation.

Could you check with a code-block similar to the following

def Reclass(fld):
if fld=='soil or fill':
return 1
elif fld=='alluvium':
return 2
else:
return 9

Think Location
8 Replies
MVP Esteemed Contributor

!= means "not equal to"

== means "equal to"

I think you have put != instead of == ?

Think Location
by
New Contributor III

Thank you, that makes sense. I replaced as suggest and the expression is still valid but now get a run error 000539 (attached).

MVP Frequent Contributor

please format your code, you're also not showing the entire code block.  It makes it very tedious to try and help you Code formatting ... the Community Version - Esri Community

Can't even see line 42 of your code.

I would add an else at the end to take into account misspellings etc. set it as something like 999,-999 or whatever.

else:
a = 999
return a

MVP Esteemed Contributor

Putting the whole codeblock here would help.

Ensure that you have returned variable a with appropriate indentation.

Could you check with a code-block similar to the following

def Reclass(fld):
if fld=='soil or fill':
return 1
elif fld=='alluvium':
return 2
else:
return 9

Think Location
by
New Contributor III

Okay I apologize for that. I couldn't fit the vertical extent of the code block into Snip so I've attached the top and bottom halves.

In any case, I incorporated your ideas and the code now works!  Thank you for the insights!

MVP Esteemed Contributor

For your future queries, here is How to insert code in your post - Esri Community

Think Location
by
New Contributor II

In the code that was submitted is an elif statement needed for all values even if they are to be classified into the same category? I am seeing at least 7 values where a=4 but am wondering if all of these classes that will be reclassified could be included in the same piece of code? I am trying to do something similar so seeing if there is a more concise way of writing the code block

New Contributor

A bit late, but maybe this will help others.  The original code needed first to have != changed to == (as mentioned above).  But once you do that, you're right, you could combine all of the statements that wanted to set the value to 4 by using a list and then checking for membership in the list.  For example, you could do something like this:

four_type_soils = ["Till", "Paleasol", "Clay"]
if fld == "Soil or Fill":
a = 1
elif fld == "Alluvium":
a = 2
elif fld == "Loess":
a = 3
elif fld in four_type_soils:
a = 4
else:
a = 9