Hi all,
This one should be an easy.
I have a table from Collector with recorded manhole Invert depths.
When I perform simple subtraction [LID_LEVEL] - [INV1_DEPTH] I get Invert Levels replicating Lid Levels if Invert Depth is zero. I want to return zero value in those cases.
The easiest way to fix this (after the fact) is to take advantage of the fact that the Calculate Field tool only operates on selected records. Open the attribute table, select for all records where your invert depth is zero, and calculate your results field to zero.
You can also get fancier with Calculate Field with a code block (see the help), but it is easier to use selections.
thanks, it did the job! I might need to automate this process with some code if I start getting more data.if anyone can assist with that, it would be great!
You can still use the field calculator if you just have one condition to test.
I have substituted your field names with 'a' and 'b' in the example, then wrapped it up with them
I am sure you can follow along. The condition is on the right, the False, True slice is on the left
... [do this if False, do this if True][condition to check] .... is the syntax
a <SPAN class="operator token">=</SPAN> <SPAN class="number token">10</SPAN> b <SPAN class="operator token">=</SPAN> <SPAN class="number token">11</SPAN> <SPAN class="punctuation token">[</SPAN>a <SPAN class="operator token">-</SPAN> b<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>a <SPAN class="operator token">-</SPAN> b <SPAN class="operator token"><=</SPAN><SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="comment token"># returns zero</SPAN> <SPAN class="comment token"># try a positive</SPAN> a <SPAN class="operator token">=</SPAN> <SPAN class="number token">10</SPAN> b <SPAN class="operator token">=</SPAN> <SPAN class="number token">9</SPAN> <SPAN class="punctuation token">[</SPAN>a <SPAN class="operator token">-</SPAN> b<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>a<SPAN class="operator token">-</SPAN>b <SPAN class="operator token"><=</SPAN><SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="comment token"># returns the difference</SPAN> <SPAN class="comment token"># now it looks horrible with your field names, but so what</SPAN> <SPAN class="comment token"># NOTE use the Python parser... field names enclosed in !'s</SPAN> <SPAN class="comment token">#</SPAN> <SPAN class="punctuation token">[</SPAN>!LID_LEVEL! <SPAN class="operator token">-</SPAN> !INV1_DEPTH!<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>!LID_LEVEL! <SPAN class="operator token">-</SPAN> !INV1_DEPTH! <SPAN class="operator token"><=</SPAN><SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Oh I get it. You are casting boolean expressions to integer in the context of a slice index, where True and False will cast to 1 and 0.
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> int<SPAN class="punctuation token">(</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> int<SPAN class="punctuation token">(</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> a <SPAN class="operator token">-</SPAN> b <SPAN class="operator token">>=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="token boolean">False</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> int<SPAN class="punctuation token">(</SPAN>a <SPAN class="operator token">-</SPAN> b <SPAN class="operator token">>=</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="number token">0</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I guess I'd rather create the code block. Readability counts.
Calculate FIeld Expression
f<SPAN class="punctuation token">(</SPAN>!LID_LEVEL! <SPAN class="operator token">-</SPAN> !INV1_DEPTH!<SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Calculate Field Code block ("pre-logic script code")
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">f</SPAN><SPAN class="punctuation token">(</SPAN>lev<SPAN class="punctuation token">,</SPAN> dep<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> lev <SPAN class="operator token">></SPAN> dep<SPAN class="punctuation token">:</SPAN> val <SPAN class="operator token">=</SPAN> lev <SPAN class="operator token">-</SPAN> dep <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> val <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="keyword token">return</SPAN> val<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
If it is simple like a two case condition check for the field calculator, I like the direct solution.
With a code block you can make a mistake with the expression or the code block. like
f(!LID_LEVEL! <SPAN>-</SPAN> !INV1_DEPTH!<SPAN>) should be </SPAN>
!LID_LEVEL! <SPAN>-</SPAN> !INV1_DEPTH!<SPAN>) should be </SPAN>
f(!LID_LEVEL!, !INV1_DEPTH!<SPAN>)</SPAN>
!LID_LEVEL!, !INV1_DEPTH!<SPAN>)</SPAN>
<SPAN><IMG src="https://us.v-cdn.net/6038851/uploads/legacyfs/online/emoticons/wink.png" /></SPAN>
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.