<?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: Calculate Field with Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175003#M13468</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;you seem to be trying to use the calculate field tool, which is normally used in a script, although exclusively.&amp;nbsp; I was referring to the field calculator which you access from an open table as per usual procedures&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/350061_field_calc03.png" style="width: 282px; height: 391px;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 08 May 2017 12:08:05 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2017-05-08T12:08:05Z</dc:date>
    <item>
      <title>Calculate Field with Python</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175000#M13465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello everyone&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a question concerning Python in ArcMap:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I want to do: I want to write a "2" in the field "Hauptgesellschaft_Gew" if the field "Hauptgesellschaft" contains the number "1235" (both fields are in the table "Wald_Poly"). This is part of an exercise given to us in which we have to calculate a suitability map.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I have tried in the Tool "Calculate Field":&lt;/P&gt;&lt;P&gt;Input Table: Wald_Poly&lt;/P&gt;&lt;P&gt;Field Name: Hauptgesellschaft_Gew&lt;/P&gt;&lt;P&gt;Expression: reclass(!Hauptgesellschaft!)&lt;/P&gt;&lt;P&gt;Expression Type (optional): PYTHON&lt;/P&gt;&lt;P&gt;Code Block (optional): inTable = "Wald_Poly"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fieldName = "Hauptgesellschaft_Gew"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;expression = "reclass(!Hauptgesellschaft!)"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;codeblock = """def reclass(Hauptgesellschaft):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if Hauptgesellschaft == '1235':&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return '2'"""&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The field formats are text/string for "Hauptgesellschaft" and short integer for "Hauptgesellschaft_Gew".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately it does not work, the programm runs, but there are no numbers written into the field "Hauptgesellschaft_Gew". We never had an introduction to Python so my (not working) solution is based on different examples I found on the internet.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The reason for this rather complicated way of solving this problem is, that I have to do everything in the model builder and that there are more complicated codes following. So if any of you know, how I could integrate an OR (like in if Hauptgesellschaft == '1235' OR Hauptgesellschaft == '1236' OR Hauptgesellschaft == '1237' ) I would be glad too.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would be really happy, if somebody could help me. I tried for hours, but I could not get it working.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;P.S. This is the first time I post something on this board, I am sorry, if I did something wrong.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 07 May 2017 19:20:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175000#M13465</guid>
      <dc:creator>AaronSchnydrig</dc:creator>
      <dc:date>2017-05-07T19:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with Python</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175001#M13466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;some issues... first you said the number 2 should be written into the new field... '2' is not a number but a text representation of the number.... second, you don't want anything to happen if the condition isn't met?&amp;nbsp; That may be fine, it will leave null values in a geodatabase field, but it will leave 0 for shape fields.&lt;/P&gt;&lt;P&gt;So, check that the field type is indeed a number field (ie long, short for no decimals) or make sure it is a text field if a string. &amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, test your code block in a sample field calculation to ensure that your indentation and everything is correct, then copy and paste and enclose in the triple quotes.... use python9.3 as well&lt;/P&gt;&lt;P&gt;All for now&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 07 May 2017 20:07:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175001#M13466</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-05-07T20:07:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with Python</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175002#M13467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;First of all, thank you very much for your answer.&lt;/P&gt;&lt;P&gt;"Hauptgesellschaft" is a text (so I continue writing '1235'), "Hauptgesellschaft_Gew" is a short integer (so I write 2 instead of '2')&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I do want something to happen if the condition isn't met, but first I will try to understand with one condition.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What do you mean by "test your code block in a sample field calculation to ensure that your indentation and everything is correct"? How can I do that? But I am quite sure, that my indentation is NOT correct. As I said, I really have no clue at all with Python, that is why I came here, to ask what part of my code is wrong.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I run my programm, get this error message:&lt;/P&gt;&lt;P&gt;ERROR 000539: Error running expression: reclass(u"1835") &lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;&amp;nbsp; File "&amp;lt;expression&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;BR /&gt;NameError: name 'reclass' is not defined&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Failed to execute (Calculate Field).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 08 May 2017 09:48:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175002#M13467</guid>
      <dc:creator>AaronSchnydrig</dc:creator>
      <dc:date>2017-05-08T09:48:15Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with Python</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175003#M13468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;you seem to be trying to use the calculate field tool, which is normally used in a script, although exclusively.&amp;nbsp; I was referring to the field calculator which you access from an open table as per usual procedures&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/350061_field_calc03.png" style="width: 282px; height: 391px;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 08 May 2017 12:08:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175003#M13468</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-05-08T12:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with Python</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175004#M13469</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This may help when trying to see the association between the field calculator expressions and how to use the CalculateField tool&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/350070_field_calc04.png" style="width: 620px; height: 376px;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 08 May 2017 13:42:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175004#M13469</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-05-08T13:42:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with Python</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175005#M13470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much, with your help I was able to solve this problem. One main mistake was that I did not type enough spaces in front of the "if" and "return" lines.&lt;/P&gt;&lt;P&gt;My final solution, just in case somebody will have the same problem in the future:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Input Table: Wald_Poly&lt;/P&gt;&lt;P&gt;Field Name: Hauptgesellschaft_Gew&lt;/P&gt;&lt;P&gt;Expression: reclass(!Hauptgesellschaft!)&lt;/P&gt;&lt;P&gt;Expression Type (optional): PYTHON_9.3&lt;/P&gt;&lt;P&gt;Code Block (optional):&lt;/P&gt;&lt;P&gt;def reclass(crit):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (crit == '1'):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif (crit == '2' or crit == '1235' or crit == '2300' or crit == '3207' or crit == '3212' or crit == '3225' or crit == '4400' or crit == '4465' or crit == '4500' or crit == '4565' or crit == '4600' or crit == '4601' or crit == '4606' or crit == '4607' or crit == '4608' or crit == '5720' or crit == '6900' or crit == '7000' or crit == '7102' or crit == '7108' or crit == '7200'):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 2&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 1&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 May 2017 11:15:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175005#M13470</guid>
      <dc:creator>AaronSchnydrig</dc:creator>
      <dc:date>2017-05-11T11:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Field with Python</title>
      <link>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175006#M13471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A little shorter version could be this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;reclass&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;crit&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; crit &lt;SPAN class="operator token"&gt;==&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'1'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;elif&lt;/SPAN&gt; crit &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'2'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'1235'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'2300'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'3207'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'3212'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'3225'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'4400'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&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;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="string token"&gt;'4465'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'4500'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'4565'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'4600'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'4601'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'4606'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'4607'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&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;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="string token"&gt;'4608'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'5720'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'6900'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'7000'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'7102'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'7108'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'7200'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;2&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;else&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:01:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculate-field-with-python/m-p/175006#M13471</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T09:01:54Z</dc:date>
    </item>
  </channel>
</rss>

