Select to view content in your preferred language

Populating attribute table with IF statement

1838
7
09-22-2021 02:50 AM
EmmaE1
by
Occasional Contributor

Hi all, 

Apologies for what is probably a very simple question with an easy solution. 

I am trying to populate a field of my attribute table using an if statement. Specifically, I have a field with vegetation types (1 - 10) but I want a new field where each of those values is assigned a name (i.e. forest cover). So If my cell value is 1, I want the new field to be assigned a value 'forest cover'. I've tried the following piece of code similar to previous answers but it is not working. Can anyone help me with a solution? 

Thank you!

def customReplace(x):
if x=='1':
return 'ForestCover'
elif x=='2':
return 'ShrubCover'
elif x=='3':
return 'Grassland'
elif x=='4':
return 'Cropland'
elif x=='5':
return 'Floodland'
elif x=='6':
return 'SparseVeg'
elif VegType =='7':
return 'Bare'
elif x=='8':
return 'Built'
elif x=='10':
return 'Water'
else:
return "Not Found"

 

0 Kudos
7 Replies
DavidPike
MVP Frequent Contributor

formatting your code will help spot syntax issues Code formatting ... the Community Version - Esri Community

Are the values 1-10 possibly integers and need the single quotes removed:

if x == 1:
  return 'ForestCover'
RobertBorchert
Honored Contributor

Alternately you can create a domain out of the vegetation types and assign that domain to the attribute with the vegetation  types.

However, as you only have a very few choices why not just do a query and update the new attribute manually. It would take less time than it does to write to code

Or if you switch to Arcade the code would look like this

 

iif($feature.[attribute] = 1, 'Forest Cover', iif($feature.[attribute] = 2 , 'Shrub Cover', etc etc...))

Just continue the second value until you get to 10 then make the final value Not Found instead of a new iff statement. 

RobertBorchert
Honored Contributor

Or create a look up table as a .csv or .txt file.  Bring it into your project, to a join and populate the text field.

jcarlson
MVP Esteemed Contributor

I like a good lookup table myself. If you don't have a text file handy for this, you can also just establish a dict of values for your lookup, thus bypassing external files, joins, and nested conditionals altogether.

PS, the post has an arcade tag on it, but the presence of "elif" suggests this is Python. @EmmaE1 could you clarify your coding language?

In Python:

 

def customReplace(x):

    values = {
        '1': 'ForestCover',
        '2': 'ShrubCover',
        '3': 'Grassland',
        '4': 'Cropland',
        '5': 'Floodland',
        '6': 'SparceVeg',
        '7': 'Bare',
        '8': 'Built',
        '10': 'Water'
    }

    return values[x]

 

 

In Arcade:

var a = $feature.FLOORCOUNT

var values = {
    '1': 'ForestCover',
    '2': 'ShrubCover',
    '3': 'Grassland',
    '4': 'Cropland',
    '5': 'Floodland',
    '6': 'SparceVeg',
    '7': 'Bare',
    '8': 'Built',
    '10': 'Water'
}

return values[a]

 

- Josh Carlson
Kendall County GIS
0 Kudos
JohannesLindner
MVP Frequent Contributor

Just as a side note, nesting 10 IIFs is bound to get complicated with the closing parantheses. It's much easier to do

var att = $feature.Attribute
return When(att==1, "ForestCover", att==2, "ShrubCover", att=3, "Grassland", "Not Found")

 


Have a great day!
Johannes
RobertBorchert
Honored Contributor

Agreed, but on a different side note.  Why bother to create a line of code to to the calculation for only 10 values?

Set up the Query in Pro starting at 1 and manually field calculate the values. For that few values it would take longer to write the code.  However, 10 iffs = 10 opening parenthesis which would equal 10 closing. 

 

JohannesLindner
MVP Frequent Contributor

Sure, doing it manually is very feasible, here. Either with a query or with Select By Attribute.


Have a great day!
Johannes
0 Kudos