<?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 Using a code block to Calculate Field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-a-code-block-to-calculate-field/m-p/318714#M24793</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi. I'm trying to do something simple in Arc but with no luck and was hoping someone could point out my error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to use the value of pixels (Value field) in a raster to define a new field called 'TreeClass', where 6 classes will be represented.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I built the attribute table and added the field (tried both short integer and long integer, the latter which matches the Value field type).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I use the following in the Code Block parameter for the Calculate Field tool:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def getclass(Value):
 if Value &amp;gt;= 0 &amp;amp; Value &amp;lt;= 10:
&amp;nbsp; return 1
 if Value &amp;gt;= 11 &amp;amp;&amp;nbsp; Value &amp;lt;=20:
&amp;nbsp; return 2
 if Value &amp;gt;= 21 &amp;amp; Value &amp;lt;= 40:
&amp;nbsp; return 3
 if Value &amp;gt;= 41 &amp;amp; Value &amp;lt;=60:
&amp;nbsp; return 4
 if Value &amp;gt;= 61 &amp;amp; Value &amp;lt;=100:
&amp;nbsp; return 5
 else:
&amp;nbsp; return 6&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And enter "getclass(!Value!)" into the Expression parameter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run the tool, the TreeClass field populates with 1's.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What am I doing wrong here? Any advice would be greatly appreciated. Thank you!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 08 Oct 2013 18:58:10 GMT</pubDate>
    <dc:creator>MargaretWooten</dc:creator>
    <dc:date>2013-10-08T18:58:10Z</dc:date>
    <item>
      <title>Using a code block to Calculate Field</title>
      <link>https://community.esri.com/t5/python-questions/using-a-code-block-to-calculate-field/m-p/318714#M24793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi. I'm trying to do something simple in Arc but with no luck and was hoping someone could point out my error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to use the value of pixels (Value field) in a raster to define a new field called 'TreeClass', where 6 classes will be represented.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I built the attribute table and added the field (tried both short integer and long integer, the latter which matches the Value field type).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I use the following in the Code Block parameter for the Calculate Field tool:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;def getclass(Value):
 if Value &amp;gt;= 0 &amp;amp; Value &amp;lt;= 10:
&amp;nbsp; return 1
 if Value &amp;gt;= 11 &amp;amp;&amp;nbsp; Value &amp;lt;=20:
&amp;nbsp; return 2
 if Value &amp;gt;= 21 &amp;amp; Value &amp;lt;= 40:
&amp;nbsp; return 3
 if Value &amp;gt;= 41 &amp;amp; Value &amp;lt;=60:
&amp;nbsp; return 4
 if Value &amp;gt;= 61 &amp;amp; Value &amp;lt;=100:
&amp;nbsp; return 5
 else:
&amp;nbsp; return 6&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And enter "getclass(!Value!)" into the Expression parameter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run the tool, the TreeClass field populates with 1's.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What am I doing wrong here? Any advice would be greatly appreciated. Thank you!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Oct 2013 18:58:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-code-block-to-calculate-field/m-p/318714#M24793</guid>
      <dc:creator>MargaretWooten</dc:creator>
      <dc:date>2013-10-08T18:58:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using a code block to Calculate Field</title>
      <link>https://community.esri.com/t5/python-questions/using-a-code-block-to-calculate-field/m-p/318715#M24794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;&amp;amp; is used in VB usually, in python is has a different function that you are probably not interested in.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try this instead.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def getclass(Value):
 if Value &amp;gt;= 0 and Value &amp;lt;= 10:
&amp;nbsp; return 1
 elif Value &amp;gt;= 11 and&amp;nbsp; Value &amp;lt;=20:
&amp;nbsp; return 2
 elif Value &amp;gt;= 21 and Value &amp;lt;= 40:
&amp;nbsp; return 3
 elif Value &amp;gt;= 41 and Value &amp;lt;=60:
&amp;nbsp; return 4
 elif Value &amp;gt;= 61 and Value &amp;lt;=100:
&amp;nbsp; return 5
 else:
&amp;nbsp; return 6
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could also shorten it using this format.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def getclass(Value):
 if 0 &amp;lt;= Value &amp;lt;= 10:
&amp;nbsp; return 1
 elif 10 &amp;lt; Value &amp;lt;= 20:
&amp;nbsp; return 2
 elif 20 &amp;lt; Value &amp;lt;= 30:
&amp;nbsp; return 3
 elif 30 &amp;lt; Value &amp;lt;= 60:
&amp;nbsp; return 4
 elif 60 &amp;lt; Value &amp;lt;= 100:
&amp;nbsp; return 5
 else:
&amp;nbsp; return 6
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:09:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-code-block-to-calculate-field/m-p/318715#M24794</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T15:09:17Z</dc:date>
    </item>
    <item>
      <title>Re: Using a code block to Calculate Field</title>
      <link>https://community.esri.com/t5/python-questions/using-a-code-block-to-calculate-field/m-p/318716#M24795</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Matthew! Looks like I've been away from python for too long.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Oct 2013 13:54:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-code-block-to-calculate-field/m-p/318716#M24795</guid>
      <dc:creator>MargaretWooten</dc:creator>
      <dc:date>2013-10-09T13:54:32Z</dc:date>
    </item>
  </channel>
</rss>

