TransWikia.com

Why does Python if/else statement only return else condition when trying to reclassify field using ArcMap field calculator?

Geographic Information Systems Asked on November 21, 2021

Using ArcGIS 10.4. I’m trying to reclassify values from a field (ECOREGION_DIVISION), which is in string format, into new values using the following Python syntax:

def Reclass(ECOREGION_DIVISION):
  if (ECOREGION_DIVISION == "Desert"):
    return "Not forest"
  else:
    return "Forest"
 
    Reclass(!ECOREGION_DIVISION!)

The result is that all the values in the new_class field are returned as "Forest" (the if statement does not return "Not forest" when value = "Desert"). The new_class field is also string format. Does this syntax seem correct?
enter image description here

One Answer

Solution:

I ended up creating a field of the class codes rather than the Ecoregion text as the input (as either a string or integer to test out). I made sure to create syntax according to the new fields (string vs integer). The Reclass function now classifies as either a text or number field (important to use quotes for the string).

Here is the python syntax for string reclass:

def Reclass(new_class):
  if new_class == "240":
    return "Marine"
  else:
    return "Not Marine"

Reclass( !new_class!)

enter image description here

And this is the syntax for a number reclass:

def Reclass(new_code):
  if new_code == 240:
    return 1
  else:
    return 2

Reclass( !new_code!)

enter image description here

I'm not exactly sure if the reason this works now is because I am using the class code field rather than the class field itself, or due to some other tweaking such as setting up correct field types.

Answered by kent on November 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP