Select to view content in your preferred language

# Calculate with <Null> in ArcGISPro Python

764
4
03-30-2023 06:30 AM
Labels (1)
Emerging Contributor

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.

1 Solution

Accepted Solutions
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.

4 Replies
Frequent Contributor

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

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.

Emerging Contributor

Thank you!

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...