<?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: Adding two attribute fields together in an empty third field. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/adding-two-attribute-fields-together-in-an-empty/m-p/154031#M11894</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In addition the points pointed out correctly by Jake, there are a few other things:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) The "env.workspace" is pointing to a featureclass (shapefile) instead of a workspace. Better change this to &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = "K:/Projects/Project Number"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) it is OK to check for the existence of the featureclass "shapefile.shp", but it won't work when the workspace is not correctly set&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;change:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;verification = arcpy.Exists(fc)
print verification
del verification
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;to:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if arcpy.Exists(fc):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # continue here with indented code&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3) You import "fileinput, os, string", but since you don't use them, it may be better not to import those.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;4) It is not necessary to delete the cursor since you are using a "with" statement.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;5) it may be wise to check if the column exists before adding the field to avoid errors&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;outFld = "Field3"
if len(arcpy.ListFields(fc, outFld)) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Addfield_management (fc, outFld, "DOUBLE")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Kind regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Xander&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 08:13:41 GMT</pubDate>
    <dc:creator>XanderBakker</dc:creator>
    <dc:date>2021-12-11T08:13:41Z</dc:date>
    <item>
      <title>Adding two attribute fields together in an empty third field.</title>
      <link>https://community.esri.com/t5/python-questions/adding-two-attribute-fields-together-in-an-empty/m-p/154029#M11892</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, fileinput, os, string
from arcpy import env
env.overwriteOutput = True
env.workspace = "K:/Projects/Project Number/Points.shp"
fc = "shapefile.shp"
verification = arcpy.Exists (fc)
print verification
del verification

arcpy.Addfield_management (fc, "Field3", "DOUBLE", "", "", "", "", "", "", "")
fields = ("Field1", "Field2", "Field3")
with arcpy.da.UpdateCursor (fc, fields) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = row[2+1]&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row)
del row
del cursor&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hello all.&amp;nbsp; I am attempting to use python to add a new attribute table column to the existing attribute table columns of a shapefile.&amp;nbsp; I have gotten this bit of code to work, as seen above and entitled "Field3."&amp;nbsp; I want to populate values in this new field by adding the values of two other fields, "Field1", and "Field2".&amp;nbsp; In other words, Field1+Field2=Field3.&amp;nbsp; I think I am almost there, but I suspect that my "row[2] = row[0+1]" is incorrect.&amp;nbsp; Any help would be extremely appreciated!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Oct 2013 14:23:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-two-attribute-fields-together-in-an-empty/m-p/154029#M11892</guid>
      <dc:creator>BrendanWhite</dc:creator>
      <dc:date>2013-10-30T14:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: Adding two attribute fields together in an empty third field.</title>
      <link>https://community.esri.com/t5/python-questions/adding-two-attribute-fields-together-in-an-empty/m-p/154030#M11893</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You will just need to make a couple line changes:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;with arcpy.da.UpdateCursor (fc, fields) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[2] = row[0] + row[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row)
del row
del cursor&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Since 'Field3' is specified 3rd in your 'fields' variable, this should be referenced by 'row[2]'.&amp;nbsp; Also, be sure to indent the lines after 'for row in cursor'.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:13:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-two-attribute-fields-together-in-an-empty/m-p/154030#M11893</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T08:13:39Z</dc:date>
    </item>
    <item>
      <title>Re: Adding two attribute fields together in an empty third field.</title>
      <link>https://community.esri.com/t5/python-questions/adding-two-attribute-fields-together-in-an-empty/m-p/154031#M11894</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In addition the points pointed out correctly by Jake, there are a few other things:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) The "env.workspace" is pointing to a featureclass (shapefile) instead of a workspace. Better change this to &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = "K:/Projects/Project Number"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) it is OK to check for the existence of the featureclass "shapefile.shp", but it won't work when the workspace is not correctly set&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;change:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;verification = arcpy.Exists(fc)
print verification
del verification
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;to:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if arcpy.Exists(fc):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # continue here with indented code&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3) You import "fileinput, os, string", but since you don't use them, it may be better not to import those.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;4) It is not necessary to delete the cursor since you are using a "with" statement.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;5) it may be wise to check if the column exists before adding the field to avoid errors&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;outFld = "Field3"
if len(arcpy.ListFields(fc, outFld)) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Addfield_management (fc, outFld, "DOUBLE")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Kind regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Xander&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:13:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-two-attribute-fields-together-in-an-empty/m-p/154031#M11894</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T08:13:41Z</dc:date>
    </item>
  </channel>
</rss>

