Select to view content in your preferred language

Row in table not updating with cursor (HELP)

861
5
Jump to solution
03-30-2022 11:40 AM
AndreaGG6
New Contributor II

Hello everyone! I created a feature class and I'm trying to update the attribute table with cursors. I created a list of coordinates (LatY) that I want to input into the "Latitude" column on my table, code works, however, my table isn't updating.

I'm attaching my code plus a screenshot of my table. I appreciate the help!!

PD: I know I can update those fields in an easier way, but the purpose of my project is to create everything using code

Screenshot_2.png

import arcpy
import numpy as np 
import pandas as pd

arcpy.env.workspace = r"C:\Users\andre\Desktop\Project\NewCores"
fc2 = r"C:\Users\andre\Desktop\Project\NewCores.shp"
field1 = ["Core_ID"]
field2 = ["Latitude"]
field3 = ["Longitude"]
field4 = ["Depth"]
arcpy.AddField_management(fc2, "Core_ID","TEXT")
arcpy.AddField_management(fc2, "Latitude","LONG")
arcpy.AddField_management(fc2, "Longitude","LONG")

LatY = (30.11386111,30.11369444,30.11380556,30.109,30.11022222,30.11175,
        30.10355556,30.10380556,30.10352778,30.10366667,30.10558333,30.10436111)

#Write code to get latitude
cursor = arcpy.da.UpdateCursor(fc2, field2)
for row in cursor:
    if row[0] in LatY:
        row[1] = LatY[row[0]]
        cursor.updateRow(row)

del cursor

 

Tags (3)
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

I don't understand row[1] as you only have one field so row will only have one element row[0].

If you are trying to populate the 12 values on row at a time (assuming they come out in order, which isn't guaranteed, depending on data source) I think this is the pattern you want:

 

 

with arcpy.da.UpdateCursor(fc2, field2) as rows:
    k = 1
    for row in rows:
        row[0] = LatY[k]
        rows.updateRow(row)
        k += 1

 

 

If order becomes a problem you can use the ID field:

 

 

with arcpy.da.UpdateCursor(fc2, ["ID", "Latitude"]) as rows:
    for row in rows:
        row[1] = LatY[row[0] - 1] # index 0 to 11 (IDs are 1 to 12)
        rows.updateRow(row)

 

One more issue, your latitude field must be FLOAT or DOUBLE not LONG (LONG will truncate all data to integer when you assign it to the field!)

View solution in original post

5 Replies
curtvprice
MVP Esteemed Contributor

I don't understand row[1] as you only have one field so row will only have one element row[0].

If you are trying to populate the 12 values on row at a time (assuming they come out in order, which isn't guaranteed, depending on data source) I think this is the pattern you want:

 

 

with arcpy.da.UpdateCursor(fc2, field2) as rows:
    k = 1
    for row in rows:
        row[0] = LatY[k]
        rows.updateRow(row)
        k += 1

 

 

If order becomes a problem you can use the ID field:

 

 

with arcpy.da.UpdateCursor(fc2, ["ID", "Latitude"]) as rows:
    for row in rows:
        row[1] = LatY[row[0] - 1] # index 0 to 11 (IDs are 1 to 12)
        rows.updateRow(row)

 

One more issue, your latitude field must be FLOAT or DOUBLE not LONG (LONG will truncate all data to integer when you assign it to the field!)

AndreaGG6
New Contributor II

Thank you so much for your help! the first code didn't work and ended up with "Line 27: row[0] = LatY[k] IndexError: tuple index out of range". 

The second one worked!  I created more fields now and it works with "Double". One more question, I have to update another field with 'TEXT', would these two codes work with that data type?

0 Kudos
curtvprice
MVP Esteemed Contributor
Ah, I should have started with k=0 for the first example (I will fix that for the good of the thread)
You can assign text, but you need to make sure the value you are assigning to a text type field is text. Python handles data types for you.
You do know that assigning latitude will not set the point location, right? You'd have to use the shape field to do that (kind of advanced stuff).
HuubZwart
Occasional Contributor

I'm not exactly sure what you're trying to accomplish here. Are you trying to check if the actual geometry lattitude is in your list and then update the new field "Lattitude" accordingly?  If so, you will need to specify the feature geometry field (using "SHAPE@", or "SHAPE@XY", for example) and use that instead. 

As to why it's not updating anything: currently in line 21 of your script, you're checking for each row if the value for field2 ("Lattitude"), which I assume is empty since you're adding the field as a new field, is in your list LatY. This is probably why nothing is updated. 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

Beyond the calculations options...

The table might not update if the table is open.  So you might want to close the table, run the script, then open the table to see if that is the issue.


... sort of retired...
0 Kudos