Hi, I've been trying to access a non-spatial hosted table on ArcGIS Online with python and have been stuck here. I can search for the table and access its table id but I've been unable to access any of the table's data. I only want to access the table's data, not edit and re-upload the table.
table = GIS.content.search('title: User List', 'Feature Layer')[0]
table_id = table.id
info = search_GIS.content.get(table_id)
Since I only need one column of the table, I first thought of extracting that column as a list and tried to query it but with the line below but I get the following Attribute Error:
table_query = info.query(out_fields='UserName')
AttributeError: 'Item' object has no attribute 'query'
I've also tried converting the column to a spatial dataframe to work with using the line below but I get a KeyError: 'filter'.
result = info.sdf
AttributeError: 'Item' object has no attribute 'filter'
Could someone point me in the right direction to access the hosted table with python? Also, is it possible to convert it to a pandas dataframe to easily work it?
Thank you!
Solved! Go to Solution.
info is an Item, as the error message indicates. To access anything within the service, you need to use layers, and, more specifically in your case, tables. That will return a list of layers or tables, respectively, and you can access the one you need by index.
If it's a one-table service, try
info.tables[0].query(out_fields='UserName', as_df=True)
If you know the URL for the layer or table, you can also use the service URL to directly access it, rather than going through the Item.
info is an Item, as the error message indicates. To access anything within the service, you need to use layers, and, more specifically in your case, tables. That will return a list of layers or tables, respectively, and you can access the one you need by index.
If it's a one-table service, try
info.tables[0].query(out_fields='UserName', as_df=True)
If you know the URL for the layer or table, you can also use the service URL to directly access it, rather than going through the Item.
Hi Josh that worked beautifully, thank you for the help!
Hi Josh, I have another question about accessing hosted tables. Each time I run the new line of code, a new sheet is added to my hosted table in the ArcGIS Online Map Viewer Classic. Is this supposed to happen and if so, is there any way to solve this?
A new "sheet"? What does that look like? It does not sound like something that is supposed to be happening.