<?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 Field Calculator Expression Inside a Python Script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/field-calculator-expression-inside-a-python-script/m-p/353559#M27739</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, imagine there are 3 fields in a table: Field1, Field2, and and empty Field3. All I want to do is multiply Field1 * Field2 and populate the result in Field3. This is easily done in the field calculator in ArcMap, but what does the code look like in a python script? I am assuming that the arcpy tool I need is Calculate Field, but am not 100% certain. The part I am stuck on is what does the 'expression' look like in the code line below... or if a different tool is better, what is it?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;CalculateField_management (in_table, field, expression, {expression_type}, {code_block})&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 28 Oct 2013 18:10:40 GMT</pubDate>
    <dc:creator>DamonOsbourne</dc:creator>
    <dc:date>2013-10-28T18:10:40Z</dc:date>
    <item>
      <title>Field Calculator Expression Inside a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-expression-inside-a-python-script/m-p/353559#M27739</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, imagine there are 3 fields in a table: Field1, Field2, and and empty Field3. All I want to do is multiply Field1 * Field2 and populate the result in Field3. This is easily done in the field calculator in ArcMap, but what does the code look like in a python script? I am assuming that the arcpy tool I need is Calculate Field, but am not 100% certain. The part I am stuck on is what does the 'expression' look like in the code line below... or if a different tool is better, what is it?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;CalculateField_management (in_table, field, expression, {expression_type}, {code_block})&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Oct 2013 18:10:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-expression-inside-a-python-script/m-p/353559#M27739</guid>
      <dc:creator>DamonOsbourne</dc:creator>
      <dc:date>2013-10-28T18:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Expression Inside a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-expression-inside-a-python-script/m-p/353560#M27740</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Damon,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It would look something like the below:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;arcpy.CalculateField_management(featureclass, "field3", "!field1! * !field2!", "PYTHON_9.3","#")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One good trick that you can do is execute the tool in ArcMap, then go to your Results window.&amp;nbsp; Here you can right-click on the results &amp;gt; Copy As Python Snippet.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]28664[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Oct 2013 18:32:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-expression-inside-a-python-script/m-p/353560#M27740</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2013-10-28T18:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Expression Inside a Python Script</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-expression-inside-a-python-script/m-p/353561#M27741</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;String formatting in Python makes this kind of thing much easier to write and debug. For example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
expr = "!{0}! * !{1}!".format("FIELD1", "FIELD2")
arcpy.CalculateField_management(featureclass, "FIELD3", expr, "PYTHON_9.3")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There are many on the forums that urge us to always use a cursor for these calculations (for performance reasons) but I still use Calculate Field for simple expressions like this because it is less hassle for me.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:35:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-expression-inside-a-python-script/m-p/353561#M27741</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T16:35:04Z</dc:date>
    </item>
  </channel>
</rss>

