3

Trying to figure out how to get this "Headwater" column to indicate "Yes" (or 1) if the value of "NextDown_Copy" is "" and "No" (or 0) if it has any other value (eg. 7100381010).

enter image description here

I attempted to try figuring out the Python code to do this using 1 and 0 coding (although I'd rather use Yes and No) (as illustrated below) but keep getting errors. I consulted a few other posts (another one), but still wasn't able to find the problem.

enter image description here

  • 2
    Looks like you are missing the indentation in the Code box. – Sara Barnes Feb 5 '16 at 16:19
  • Another great reason to learn PostGIS...so python doesn't have to do the job that SQL was meant to do... – DPSSpatial Feb 5 '16 at 16:27
  • Sara Barnes, where should the indentation have been? – Dalal_EL_Hanna Feb 8 '16 at 0:17
6

Here's a simple expression with no need to define a function in the codeblock:

Headwater =

0 if !NextDown_Copy! else 1

This checks if each value of NextDown_Copy is truthy (i.e. not a null, 0, empty string), and returns a 0 if so, otherwise it returns a 1.

  • Thanks! This pretty much worked, although it returned 1s for Null values and left Null in all the rows for which NextDown_Copy had values. Any ideas why? – Dalal_EL_Hanna Feb 8 '16 at 0:11
  • That's very odd. What if you try 1 if !NextDown_Copy! is None else 0? – nmpeterson Feb 8 '16 at 0:17
  • Hum! Good idea. Unfortunately still didn't work! – Dalal_EL_Hanna Feb 8 '16 at 17:04
  • Is Headwater a text field? If so, try "0" if !NextDown_Copy! else "1" – nmpeterson Feb 8 '16 at 17:08
  • 1
    Works like a charm! Thanks. (Looks like today I was making a big mistake by not selecting Python while running the codes... I just realized this, which is likely what was causing the problem, sorry about this!) – Dalal_EL_Hanna Feb 8 '16 at 18:38
2

Python uses None to denote null values. I'd replace your Null with None (no quotes or brackets)and see what you get. Your script is essentially looking for a text string value of null. Furthermore, it's not contained in single quotes, so you're throwing a syntax error twice over.

  • Tried this out with no luck. But thanks, as its great to know that Python denotes nulls as None. I am totally unfamiliar with Python for the moment and was doing guess work, so indeed, I was trying to get it to search for a string value! Good to know I shouldn't have been doing that. – Dalal_EL_Hanna Feb 8 '16 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.