Select to view content in your preferred language

Access Non-Spatial Hosted Table with Python

1392
4
Jump to solution
02-03-2023 12:39 PM
by Anonymous User
Not applicable

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!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

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.

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

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.

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

Hi Josh that worked beautifully, thank you for the help!

0 Kudos
by Anonymous User
Not applicable

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?

0 Kudos
jcarlson
MVP Esteemed Contributor

A new "sheet"? What does that look like? It does not sound like something that is supposed to be happening.

- Josh Carlson
Kendall County GIS
0 Kudos