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
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
Solved! Go to Solution.
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!)
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!)
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?
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.
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.