Using Search and Insert Cursor to Find Certain Values and Replicate

1404
1
Jump to solution
10-20-2021 03:22 PM
WillemVan_Riet1
New Contributor III

I'm new to ArcPy and really coding in general.

I have three fields titled "Subcat_1", "Subcat_2", and "Subcat_3" all being used to categorize different themes of survey data that will then be used to create heat maps.

I am trying to:

  1. Use SearchCursor from the Data Access module to fetch an entire row if a value in Subcat_2 or Subcat_3 are not null (This I have working already - I changed my null values to a string "Nothing")
  2. Copy and insert that fetched row to the row below, where the populated value from Subcat_2 will replace the value in Subcat_1 (as that value is above)

EDIT I have attached screenshots of what this workflow looks like in Excel

  1. Initial Table 

     
    WillemVan_Riet1_9-1634768230773.png

     

     

     

  2. Find value that is not empty in Subcategory_2

     
    WillemVan_Riet1_10-1634768308907.png

     

  3. Copy that row 

     
    WillemVan_Riet1_11-1634768328601.png

     

  4. Insert into row below 

     
    WillemVan_Riet1_12-1634768355244.png

     

  5. Have the value in Subcat_2 replace the duplicated value in Subcat_1

     
    WillemVan_Riet1_13-1634768394312.png

     

Since I am inserting a row from the same feature class, the length, schema, geometry is all the same.

The version of some code that somewhat works is below (this is attempting to look for any unique values in Subcat_3, copying the row, and inserting them into Subcat_2):

# A list of values that will be used to construct new rows

values = [row[0] for row in arcpy.da.SearchCursor(social_layer, fieldnames)]
#social_layer is the feature class I am working on
#fieldnames is the query for "Subcat_3"

uniqueValues = set(values)

# # Open an InsertCursor
cursor = arcpy.da.InsertCursor(social_layer,
                           ['Subcat_2'])
for row in values:
    if row != 'Nothing':
        cursor.insertRow([row]) #have to insert a list not a single value

# Delete cursor object
del cursor

When I run the code I just break the layer 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Basic code would be something like this:

# read all attributes you want to copy, not just Subcat_3! Especially Shape, else you won't see the feature.
fields = ["SHAPE@", "Subcat_2", "Subcat_3"]

# use an SQL query ("Subcat_3 IS NOT NULL" or "Subcat_3 <> 'Nothing'")
values = [list(row) for row in arcpy.da.SearchCursor(social_layer, fields, "Subcat_3 IS NOT NULL")]

# I don't understand why you would want to have unique values?
# According to your question, you want to copy _every_ row with values in Subcat_3.

with arcpy.da.InsertCursor(social_layer, fields) as cursor:
    for row in values:
        # replace Subcat_2 with Subcat_3, delete Subcat_3
        row[-2] = row[-1]
        row[-1] = None
        # insert
        cursor.insertRow(row)

Have a great day!
Johannes

View solution in original post

1 Reply
JohannesLindner
MVP Frequent Contributor

Basic code would be something like this:

# read all attributes you want to copy, not just Subcat_3! Especially Shape, else you won't see the feature.
fields = ["SHAPE@", "Subcat_2", "Subcat_3"]

# use an SQL query ("Subcat_3 IS NOT NULL" or "Subcat_3 <> 'Nothing'")
values = [list(row) for row in arcpy.da.SearchCursor(social_layer, fields, "Subcat_3 IS NOT NULL")]

# I don't understand why you would want to have unique values?
# According to your question, you want to copy _every_ row with values in Subcat_3.

with arcpy.da.InsertCursor(social_layer, fields) as cursor:
    for row in values:
        # replace Subcat_2 with Subcat_3, delete Subcat_3
        row[-2] = row[-1]
        row[-1] = None
        # insert
        cursor.insertRow(row)

Have a great day!
Johannes