I don't have a whole lot of python experience and I have been trying to get this script working for a while now and it seems like I'm close to getting it to work, but at this point I feel as if I'm banging my head against the wall.
So, I have a hosted feature layer on my organization's Enterprise. It has two layers, called Private Trees and Public trees and a table called Tree Pruning Schedule that is related to the Public Trees layer. I'm trying to get the script to write attributes from the table into the layer itself. I followed this post for the most part, but my issue is once it gets to the end I get "Exception: 'list' object has no attribute 'attributes'". And I'm not sure how to fix it. Any help would be greatly appreciated!
from arcgis import GIS
gis = GIS("home")
from arcgis.features import FeatureLayer
import pandas as pd
from arcgis.features import FeatureCollection
# Call feature service and make variables for the Public Trees layer
treeinventory = gis.content.get("ID")
trees = treeinventory.layers[1]
trees_sedf = pd.DataFrame.spatial.from_layer(trees)
trees_fset = trees.query()
trees_features = trees_fset.features
# Make variable for the Pruning Table
table_url = 'org url'
table_lyr = FeatureLayer(table_url)
table_sedf = pd.DataFrame.spatial.from_layer(table_lyr)
table_fset = table_lyr.query()
table_features = table_fset.features
# Set up dataframes, drop duplicates besides most recent, merge both data frame tables
df = table_sedf.sort_values('tree_id', ascending=False)
df = df.drop_duplicates(subset="tree_id")
joined_rows = pd.merge(left = trees_sedf, right = df, how = 'inner', on = 'tree_id')
# Loop through dataframes and update Trees to match Pruning Table
def update(trees, table):
for tree_id in joined_rows['tree_id']:
try:
tree_feature = [f for f in trees if f.attributes['tree_id'] == tree_id][0]
tree_feature.attributes['prunesched'] = table.attributes['pruningschedule']
trees.edit_features(updates=[tree_feature])
except Exception as e:
print(f"Could not update {tree_id}. Exception: {e}")
continue
update(trees_features, table_features)