4

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 Answers2

7

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.

nmpeterson
  • 8,276
  • 33
  • 59
  • 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 08 '16 at 00:11
  • That's very odd. What if you try 1 if !NextDown_Copy! is None else 0? – nmpeterson Feb 08 '16 at 00:17
  • Hum! Good idea. Unfortunately still didn't work! – Dalal_EL_Hanna Feb 08 '16 at 17:04
  • Is Headwater a text field? If so, try "0" if !NextDown_Copy! else "1" – nmpeterson Feb 08 '16 at 17:08
  • I just tested all 3 of these options on my own test feature class (in a file geodatabase, where both fields are text-formatted), and all 3 of them worked as expected. How exactly is your data formatted? You're not trying to run this while you have any features selected, right? – nmpeterson Feb 08 '16 at 17:14
  • Aha! Maybe the fiels class is the problem then. My fields are 'double'. (And no, nothing is selected!) – Dalal_EL_Hanna Feb 08 '16 at 17:48
  • I'm at a complete loss as to why this is failing for you. I tested with double fields, too, and it's working for me in ArcGIS 10.2.2. – nmpeterson Feb 08 '16 at 17:57
  • Another expression you can try: (0,1)[bool(!NextDown_Copy!)]. – nmpeterson Feb 08 '16 at 18:33
  • 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 08 '16 at 18:38
  • Yeah, that would do it :) If you could mark the answer as correct, I'd really appreciate it. – nmpeterson Feb 08 '16 at 18:40
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.

Jyler
  • 1,175
  • 11
  • 26
  • 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 08 '16 at 00:16