Calculate Field, Helpers, Reclassify, If/elif

895
7
Jump to solution
10-08-2021 12:36 PM
GJB
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!

0 Kudos
3 Solutions

Accepted Solutions
JayantaPoddar
MVP Esteemed Contributor

!= means "not equal to"

== means "equal to"

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

Please replace != with ==



Think Location

View solution in original post

0 Kudos
DavidPike
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

 

View solution in original post

0 Kudos
JayantaPoddar
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

JayantaPoddar_0-1633725993951.png

 

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

 



Think Location

View solution in original post

0 Kudos
7 Replies
JayantaPoddar
MVP Esteemed Contributor

!= means "not equal to"

== means "equal to"

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

Please replace != with ==



Think Location
0 Kudos
GJB
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).

0 Kudos
DavidPike
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

 

0 Kudos
JayantaPoddar
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

JayantaPoddar_0-1633725993951.png

 

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

 



Think Location
0 Kudos
GJB
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!

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

Glad we could help!

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



Think Location
0 Kudos
SGuiryVR
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

0 Kudos