<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Omitting 0 values in script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298740#M23135</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, then in your loop you might want to first build a list of the non-zero values and calculate on those. Here's a very untested quick conceptual method. Think of it as pseudo-code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; validlist = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.A &amp;lt;&amp;gt; 0: validlist.append(row.A) # trying to think of a way to loop this part, drawing a blank
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.B &amp;lt;&amp;gt; 0: validlist.append(row.B)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.C &amp;lt;&amp;gt; 0: validlist.append(row.C)
&amp;nbsp;&amp;nbsp;&amp;nbsp; .
&amp;nbsp;&amp;nbsp;&amp;nbsp; .
&amp;nbsp;&amp;nbsp;&amp;nbsp; .
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.N &amp;lt;&amp;gt; 0: validlist.append(row.N)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = validlist[0] * Ln(1/validlist[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(1, len(validlist) + 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result += validlist&lt;I&gt; * Ln(1/validlist&lt;I&gt;)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 14:20:38 GMT</pubDate>
    <dc:creator>Zeke</dc:creator>
    <dc:date>2021-12-11T14:20:38Z</dc:date>
    <item>
      <title>Omitting 0 values in script</title>
      <link>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298737#M23132</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi guys, this is my first time posting so please forgive mistakes.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am a python newbie, and I have a census tract shapefile joined to income category data. I want to calculate an entropy value, which follows this formula:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A*Ln(1/A)+B*Ln(1/B)+C*Ln(1/C) etc. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;where A, B, C, etc are proportions of the population of each tract within each income category.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Some values are 0, and they break the formula because it divides by zero as you can see in the formula.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to automate this formula so that the user of the tool simply has to input the census data.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have thought about making a cursor that loops through a conditional statement like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row &amp;gt;= 0:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return True&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return False&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;then, I want to put all the ones that returned True into the formula and omit the false ones. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have thought about creating extra columns for every original value so each new column contains a "A*Ln(1/A)" component, and then sum only those that are not NULL, but I do not know how to make python do this. Ideas? There must be a way.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 May 2014 00:47:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298737#M23132</guid>
      <dc:creator>PolinaBerenstein</dc:creator>
      <dc:date>2014-05-01T00:47:26Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting 0 values in script</title>
      <link>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298738#M23133</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Two methods, depending on whether you want to skip the calculation altogether if any value is zero, or if you want to calculate any non-zero values, skipping only zeros. If you have a lot of values to calculate, there's probably a better way, but this will work for your example.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;edit: also, you marked your own post as answered (green check mark). Use that for whoever best answered your question. Other forum members may not read your post, assuming it's been solved.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Method 1
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if A == 0 or B == 0 or C == 0:&amp;nbsp;&amp;nbsp;&amp;nbsp; # you may actually want row.A, etc, here, or row[0], row[1]. Second method is if you use arcpy.da
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue&amp;nbsp;&amp;nbsp;&amp;nbsp; # this will skip this row in the for loop
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # do your calculations

# Method 2
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if A == 0 and B != 0 and C != 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # calculate only B and C
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif A != 0 and B != 0 and C == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # calculate only A and B
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif: # and so on, for as many combinations as you have
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:20:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298738#M23133</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2021-12-11T14:20:33Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting 0 values in script</title>
      <link>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298739#M23134</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the answer! I was using an example with only 3 columns, in reality I have about 10 values, so the combination method is probably not a good idea. From what I understand, the first method will skip the entire calculation for the row, but I just want to omit the zero value from the overall calculation.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, good call, I unchecked it...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Two methods, depending on whether you want to skip the calculation altogether if any value is zero, or if you want to calculate any non-zero values, skipping only zeros. If you have a lot of values to calculate, there's probably a better way, but this will work for your example.&lt;BR /&gt;&lt;BR /&gt;edit: also, you marked your own post as answered (green check mark). Use that for whoever best answered your question. Other forum members may not read your post, assuming it's been solved.&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Method 1
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if A == 0 or B == 0 or C == 0:&amp;nbsp;&amp;nbsp;&amp;nbsp; # you may actually want row.A, etc, here, or row[0], row[1]. Second method is if you use arcpy.da
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue&amp;nbsp;&amp;nbsp;&amp;nbsp; # this will skip this row in the for loop
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # do your calculations

# Method 2
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if A == 0 and B != 0 and C != 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # calculate only B and C
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif A != 0 and B != 0 and C == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # calculate only A and B
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif: # and so on, for as many combinations as you have
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:20:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298739#M23134</guid>
      <dc:creator>PolinaBerenstein</dc:creator>
      <dc:date>2021-12-11T14:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting 0 values in script</title>
      <link>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298740#M23135</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, then in your loop you might want to first build a list of the non-zero values and calculate on those. Here's a very untested quick conceptual method. Think of it as pseudo-code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; validlist = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.A &amp;lt;&amp;gt; 0: validlist.append(row.A) # trying to think of a way to loop this part, drawing a blank
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.B &amp;lt;&amp;gt; 0: validlist.append(row.B)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.C &amp;lt;&amp;gt; 0: validlist.append(row.C)
&amp;nbsp;&amp;nbsp;&amp;nbsp; .
&amp;nbsp;&amp;nbsp;&amp;nbsp; .
&amp;nbsp;&amp;nbsp;&amp;nbsp; .
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.N &amp;lt;&amp;gt; 0: validlist.append(row.N)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = validlist[0] * Ln(1/validlist[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(1, len(validlist) + 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result += validlist&lt;I&gt; * Ln(1/validlist&lt;I&gt;)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:20:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/omitting-0-values-in-script/m-p/298740#M23135</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2021-12-11T14:20:38Z</dc:date>
    </item>
  </channel>
</rss>

