Writing if/else statement in field calculator for calculating values

1895
1
Jump to solution
06-11-2018 03:43 PM
Brian_McLeer
Occasional Contributor II

I have referenced this article Writing conditional (if/then) statements into Field Calculator of ArcGIS for Desktop using Python pa... 

I am trying to update values in a field using 'elif' statements in Field Calculator with the Python radio button selected.

The set is as:

Code Block:

def Reclass(Density_18):
                if (Density_18 <= .8493):
                                return 1
                elif (Density_18 >.8493 and Density_18 <=1.761):
                                return 2
                elif (Density_18 >1.761 and Density_18 <=2.345):
                                return 3
                elif (Density_18 >2.345 and Density_18 <=3.399):
                                return 4
                elif (Density_18 >3.399):
                                return 5

Expression:

Reclass( !Index_18!)

When this expression is performed, it calculates all the values as '1', then the next time it is run all values are '2', so on until it maxes out and repeats at '5' in the Index_18 field. 

Brian
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Indentation is a bit off, but I tested it after reformatting using individual values and it is fine.

I suspect that the !Index1! field is the field you are trying to put the values IN and not the field that contains the Density values... check that

Python parser, as before

def Reclass(Density_18):
    if (Density_18 <= .8493):
        return 1
    elif (Density_18 >.8493 and Density_18 <=1.761):
        return 2
    elif (Density_18 >1.761 and Density_18 <=2.345):
        return 3
    elif (Density_18 >2.345 and Density_18 <=3.399):
        return 4
    elif (Density_18 >3.399):
        return 5
    

Reclass(Density_18)
 3

Density_18 = .1

Reclass(Density_18)
1

View solution in original post

1 Reply
DanPatterson_Retired
MVP Emeritus

Indentation is a bit off, but I tested it after reformatting using individual values and it is fine.

I suspect that the !Index1! field is the field you are trying to put the values IN and not the field that contains the Density values... check that

Python parser, as before

def Reclass(Density_18):
    if (Density_18 <= .8493):
        return 1
    elif (Density_18 >.8493 and Density_18 <=1.761):
        return 2
    elif (Density_18 >1.761 and Density_18 <=2.345):
        return 3
    elif (Density_18 >2.345 and Density_18 <=3.399):
        return 4
    elif (Density_18 >3.399):
        return 5
    

Reclass(Density_18)
 3

Density_18 = .1

Reclass(Density_18)
1