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:
EDIT I have attached screenshots of what this workflow looks like in Excel
Initial Table
Find value that is not empty in Subcategory_2
Copy that row
Insert into row below
Have the value in Subcat_2 replace the duplicated value in Subcat_1
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
Solved! Go to Solution.
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)
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)