<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Writing attributes from a related table to a feature layer in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1545090#M10731</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/727231"&gt;@RemyShipman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you print tree_feature before applying the edit_features(updates) what does the printout look like for tree_feature?&lt;/P&gt;&lt;P&gt;The edit_features(updates=) is expecting a list of dictionaries or in your case, a list containing one dictionary that describes the update for the feature on each iteration.&lt;/P&gt;</description>
    <pubDate>Thu, 03 Oct 2024 08:18:03 GMT</pubDate>
    <dc:creator>Clubdebambos</dc:creator>
    <dc:date>2024-10-03T08:18:03Z</dc:date>
    <item>
      <title>Writing attributes from a related table to a feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1544961#M10727</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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 &lt;A href="https://community.esri.com/t5/arcgis-online-blog/easy-how-to-symbology-using-related-records/ba-p/890426" target="_self"&gt;this post&lt;/A&gt; 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!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2024 20:52:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1544961#M10727</guid>
      <dc:creator>RemyShipman</dc:creator>
      <dc:date>2024-10-02T20:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: Writing attributes from a related table to a feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1545090#M10731</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/727231"&gt;@RemyShipman&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you print tree_feature before applying the edit_features(updates) what does the printout look like for tree_feature?&lt;/P&gt;&lt;P&gt;The edit_features(updates=) is expecting a list of dictionaries or in your case, a list containing one dictionary that describes the update for the feature on each iteration.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 08:18:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1545090#M10731</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2024-10-03T08:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: Writing attributes from a related table to a feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1545203#M10733</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/727231"&gt;@RemyShipman&lt;/a&gt;,&amp;nbsp;you might be able to do this a little easier with search and update cursors.&amp;nbsp; Below is an example:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import arcpy
from arcgis import GIS

# Variables
portalURL = 'https://portal.esri.com/portal'
username = 'portaladmin'
password = '**********'
itemID = '7dd52c5ef86b44efaaac764294abf163'

# Connect to Portal
print("Connecting to Portal")
gis = GIS(portalURL, username, password)
arcpy.SignInToPortal(portalURL, username, password)

# Reference services
print("Referencing services")
treeinventory = gis.content.get(itemID)
treeLyr = treeinventory.layers[0]
treeTbl = treeinventory.tables[0]

# Create dictionary of schedule
print("Creating dictionary of schedule")
schedDict = {}
with arcpy.da.SearchCursor(treeTbl.url, ['tree_id', 'pruningschedule']) as cursor:
    for row in cursor:
        schedDict[row[0]] = row[1]
del cursor

# Updating tree lyr
print("Updating tree")
with arcpy.da.UpdateCursor(treeLyr.url, ['tree_id', 'prunesched']) as cursor:
    for row in cursor:
        try:
            row[1] = schedDict[row[0]]
            cursor.updateRow(row)
        except KeyError:
            pass
del cursor

print("Finished")&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 03 Oct 2024 14:50:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1545203#M10733</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2024-10-03T14:50:10Z</dc:date>
    </item>
    <item>
      <title>Re: Writing attributes from a related table to a feature layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1545298#M10734</link>
      <description>&lt;P&gt;To accompany the great ArcPy solution from&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/10527"&gt;@JakeSkinner&lt;/a&gt;&amp;nbsp;here is an option using the ArcGIS API for Python in case ArcPy is not an option.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis import GIS

## Access ArcGIS Online
agol = GIS("home")

################################################################################
## USER INPUT REQUIRED #########################################################

## get the Feature Service as an Item object
trees_fs = agol.content.get("FS_ITEM_ID")

match_field_lyr = "FIELD_NAME" # Matching ID from layer
match_field_tbl = "FIELD_NAME" # Matching ID from table
fld2update = "FIELD_NAME"
tbl_fld = "FIELD_NAME"

## NOTE: You also need to put in the layer and table index below

################################################################################

## get the Feature Layer to update as a FeatureLayer object
trees_fl = trees_fs.layers[1] # get the correct layer

## get the attributes for each tree
## returns the OID, MatchField, FieldToUpdate
trees_features = trees_fl.query(
    out_fields = f"{match_field_lyr},{fld2update}",
    return_geometry = False
).features

## get the table as a Table object
trees_tbl = trees_fs.tables[0] # get the correct table

## get the attribute from the table
## return OID, MatchField, FieldToGetDataFrom
trees_records = trees_tbl.query(
    out_fields = f"{match_field_tbl},{tbl_fld}"
).features

## create a dictionary from trees_records for lookup based on MatchField
lookup = {f.attributes[match_field_lyr]: f.attributes[tbl_fld] for f in trees_records}

## iterate over trees_features and update the FieldToUpdate based on REG_NO using the lookup dictionary
for f in trees_features:
    reg_no = f.attributes[match_field_tbl]
    if reg_no in lookup:
        f.attributes[fld2update] = lookup[reg_no]
        ## you could call edit_features here too instead of below
        ## although will be significantly slower with this approach
        #trees_fl.edit_features(
        #    updates=[f]
        #)


## update the FeatureLayer
trees_fl.edit_features(
    updates=trees_features
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 17:39:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/writing-attributes-from-a-related-table-to-a/m-p/1545298#M10734</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2024-10-03T17:39:46Z</dc:date>
    </item>
  </channel>
</rss>

