Edit: this is a hosted Notebook on our AGOL org
I can't believe I cannot see any examples in the docs on simply accessing attributes of a hosted AGOL table/service, prbly just missing the obvious here. I can get to all attributes of "features" but I'm unsure how to go about getting a specific attribute for each feature but had to actually apply a query to get the list object.
import time, os
from datetime import datetime, timedelta
from arcgis.gis import GIS
gis = GIS("home")
from arcgis.features import FeatureLayer
from arcgis.features import FeatureLayerCollection
t = time.localtime()
last_date_time = datetime.utcnow() - timedelta(days = 18)
timestamp = last_date_time.strftime("%Y-%m-%d %H:%M:%S")
#timestamp = '2022-10-02 15:00:00 PM'
sunsetTabId = '5cf39cfa77694131821ec89cc1bf0150'
sunsetTabItem = gis.content.get(sunsetTabId)
fields=['itemIdToProcess','daysToKeep']
for tab in sunsetTabItem.tables:
tabSet = tab.query(where='OBJECTID>0')
print (tab.properties.name)
for row in tabSet.features:
print (row)
This iterates over the "tabSet.features" object and I can print each row but unsure how to get at each "itemId" value each iteration.
{"attributes": {"OBJECTID": 1, "itemId": "416c10d63b78464798abc3c10e70c71b", "daysToKeep": 10}}
{"attributes": {"OBJECTID": 2, "itemId": "105f5c3e534744da9df8a0d8953fffc2", "daysToKeep": 5}}
{"attributes": {"OBJECTID": 3, "itemId": null, "daysToKeep": 1}}
{"attributes": {"OBJECTID": 4, "itemId": null, "daysToKeep": 1}}
{"attributes": {"OBJECTID": 5, "itemId": null, "daysToKeep": 1}}
{"attributes": {"OBJECTID": 6, "itemId": null, "daysToKeep": 1}}
{"attributes": {"OBJECTID": 7, "itemId": null, "daysToKeep": 1}}
{"attributes": {"OBJECTID": 8, "itemId": null, "daysToKeep": 1}}
{"attributes": {"OBJECTID": 9, "itemId": null, "daysToKeep": 1}}
{"attributes": {"OBJECTID": 10, "itemId": null, "daysToKeep": 1}}
{"attributes": {"OBJECTID": 11, "itemId": null, "daysToKeep": 1}}
But I cannot get "itemId"?
I've tried several combinations of things like:
for row in qResult.features:
print (row.attributes.itemId)
But gives
AttributeError: 'dict' object has no attribute 'itemId'
Solved! Go to Solution.
The feature isn't subscriptable, but its attributes property (a dict) is.
for row in tabSet:
print(row.attributes['itemId'])
row['itemId']
TypeError Traceback (most recent call last) Input In [143], in <cell line: 20>() 21 tabSet = tab.query(where='1=1') 22 for row in tabSet: ---> 23 print (row['itemId']) TypeError: 'Feature' object is not subscriptable
The feature isn't subscriptable, but its attributes property (a dict) is.
for row in tabSet:
print(row.attributes['itemId'])
That works. Thanks -- I really was questioning if I had to actually iterate the dataframe! This will def simplify it a level or two.
I guess I have to use the pandas dataframe object of the tabSet?
for tab in sunsetTabItem.tables:
tabSet = tab.query(where='1=1',as_df=True)
for index, row in tabSet.iterrows():
print (row.itemId + "|" + str(row.daysToKeep))
Produces:
416c10d63b78464798abc3c10e70c71b|10
105f5c3e534744da9df8a0d8953fffc2|5
None|1
None|1
None|1
None|1
None|1
None|1
None|1
None|1
None|1
I guess I was expecting the python API to provide access to values of columns specified (ie, ".itemId")