Python Delete Cursor

815
5
Jump to solution
04-01-2021 05:59 PM
TatjanaScagliotti
New Contributor III

I want to delete specific values in a field. In the code below I used an update cursor to update Wk4_May18May21_ACTIV with the values from Wk3_May11May14_ACTIV that had the word "Planned" in them. Now I want to delete the values from Wk3_May11May14_ACTIV that have the word "Planned" in them but using a delete cursor deletes the entire row. I just want the values in the column deleted (they can be left as null or empty). Also, all of this code is being used in Jupyter notebooks and the data being changed is on AGOL.

 

with arcpy.da.UpdateCursor (fc, ['SITE_ID', 'Wk3_May11May14_ACTIV','Wk4_May18May21_ACTIV'], "Wk3_May11May14_ACTIV LIKE '%Planned%'") as cursor:
    for row in cursor:
        row[2] = row[1]
        cursor.updateRow(row)

 

Any help would be great, I am fairly new to Python.

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Regular Contributor

Then set row[1] = None.  Using the update cursor you can move the value of row[1] into row[2], then delete (set to None) the value in row[1].

with arcpy.da.UpdateCursor (fc, ['SITE_ID', 'Wk3_May11May14_ACTIV','Wk4_May18May21_ACTIV'], "Wk3_May11May14_ACTIV LIKE '%Planned%'") as cursor:
    for row in cursor:
        row[2] = row[1]
        row[1] = None
        cursor.updateRow(row)

 

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

if the code works for replacement and the query works, then did you try row[2] = None


... sort of retired...
TatjanaScagliotti
New Contributor III

I'm not trying to delete anything from row[2] it's the values in row[1] that I was to delete.

0 Kudos
DanPatterson
MVP Esteemed Contributor

same principle, switch the assignment of None


... sort of retired...
0 Kudos
RandyBurton
MVP Regular Contributor

Then set row[1] = None.  Using the update cursor you can move the value of row[1] into row[2], then delete (set to None) the value in row[1].

with arcpy.da.UpdateCursor (fc, ['SITE_ID', 'Wk3_May11May14_ACTIV','Wk4_May18May21_ACTIV'], "Wk3_May11May14_ACTIV LIKE '%Planned%'") as cursor:
    for row in cursor:
        row[2] = row[1]
        row[1] = None
        cursor.updateRow(row)

 

TatjanaScagliotti
New Contributor III

Amazing thank you! 

0 Kudos