Calculate with <Null> in ArcGISPro Python

534
4
Jump to solution
03-30-2023 06:30 AM
Labels (1)
DeepSeaMapping
New Contributor II

Hi, 

I have some troubles calculatin with <Null> values. I have two columns and a want to cearte a third one from it. Col 1 is filled with values. Col 2 has some intentional <Null>. It want Col 3 to be conditionally filled if the value in Col2  is <Null>. Below a picture of my attempts, but it seems I do not understand python enough and I didn't find an answer.

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

The below should work:

def test(x, y):
    if x == 'UNF_GT' and y == None:
        return 'UNF_GT'

Try removing the other elif statements.

View solution in original post

4 Replies
JohannesBierer
Occasional Contributor III

Would it be an idea to replace first the Null values with a real "0" and then do the calculations?

https://support.esri.com/en/technical-article/000023190

0 Kudos
JakeSkinner
Esri Esteemed Contributor

The below should work:

def test(x, y):
    if x == 'UNF_GT' and y == None:
        return 'UNF_GT'

Try removing the other elif statements.

DeepSeaMapping
New Contributor II

Thank you! 

0 Kudos
DanPatterson
MVP Esteemed Contributor

A sample calculation for something unrelated.

If the second fld (fld2 or in your case 'y') is None, then split up what you want return into little bits.

If you have too many bits, you can use a dictionary to extract the value.  Be wary of the output type, which in your case is text.  Also provide a return value if the second field is not None

def junk_cal(fld1, fld2):
    """Account for nulls"""
    if fld2 is None:
        if fld1 == 1:
            return "one"
        elif fld1 == 2:
            return "twp"
        elif fld1 == 3:
            return "three"
    return str(fld2)

 


... sort of retired...
0 Kudos