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!
Solved! Go to Solution.
!= means "not equal to"
== means "equal to"
I think you have put != instead of == ?
Please replace != with ==
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
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
!= means "not equal to"
== means "equal to"
I think you have put != instead of == ?
Please replace != with ==
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
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
Glad we could help!
For your future queries, here is How to insert code in your post - Esri Community
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
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