Calculating field values only for appropriate features using ArcMap

2520
14
Jump to solution
02-01-2020 10:50 AM
RafaelMichael
Occasional Contributor

Just like in the attached picture. I would like to calculate the value for the field "indeks_star" with the formula: indeks_star =[TOT_65__] / [TOT_0_14] * 100

And for features where in the field TOT_0_14 occurs 0 or in [TOT_65__] occurs 0, I would like the value -9999 to appear in the field indeks_star.

What formula to enter in the field calculator to simultaneously calculate all values for the field indeks_star?

0 Kudos
14 Replies
DavidPike
MVP Frequent Contributor

I'm going to let you suggest the code then we can look at it.

RafaelMichael
Occasional Contributor
def percentage(field1, field2) :

    if field1 != 0 and field2 != 0 :
        return ( (float(field1) / float(field2) ) * 100.0)

    else :
        return -9999

percentage( !TOT_65__!, !TOT!)               ?

0 Kudos
DavidPike
MVP Frequent Contributor

RafaelMichael
Occasional Contributor

values seem correct, thanks for everything!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

This type of basic calculation with a Null or zero check can be accomplished on one line using a Python conditional expressions, a.k.a., ternary operators, 

-9999 if !TOT! == 0 else !TOT_65!/float(!TOT!)*100