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).
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.
-
2Looks like you are missing the indentation in the Code box.Sara Barnes– Sara Barnes2016年02月05日 16:19:12 +00:00Commented Feb 5, 2016 at 16:19
-
Sara Barnes, where should the indentation have been?Dalal_EL_Hanna– Dalal_EL_Hanna2016年02月08日 00:17:18 +00:00Commented Feb 8, 2016 at 0:17
2 Answers 2
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– Dalal_EL_Hanna2016年02月08日 00:11:15 +00:00Commented Feb 8, 2016 at 0:11
-
That's very odd. What if you try
1 if !NextDown_Copy! is None else 0
?nmpeterson– nmpeterson2016年02月08日 00:17:47 +00:00Commented Feb 8, 2016 at 0:17 -
Hum! Good idea. Unfortunately still didn't work!Dalal_EL_Hanna– Dalal_EL_Hanna2016年02月08日 17:04:43 +00:00Commented Feb 8, 2016 at 17:04
-
Is
Headwater
a text field? If so, try"0" if !NextDown_Copy! else "1"
nmpeterson– nmpeterson2016年02月08日 17:08:23 +00:00Commented Feb 8, 2016 at 17:08 -
1Works 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– Dalal_EL_Hanna2016年02月08日 18:38:56 +00:00Commented Feb 8, 2016 at 18:38
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– Dalal_EL_Hanna2016年02月08日 00:16:38 +00:00Commented Feb 8, 2016 at 0:16
Explore related questions
See similar questions with these tags.