Output Spatial Dataframe/Dataframe to Table on ArcGIS Online

777
4
09-21-2022 06:44 AM
ZacharyStanley
New Contributor III

Hello,

I have a fairly simple notebook that takes a feature layer and converts it to an sdf and ultimately a df. I would simply like to output this as a table to ArcGIS online.  I hope it's a simple path issue, but I'm not optimistic. The other options I've explored are the REST API 'export item' which I've had mixed results with and the idea of using the add() method. The last line in bold is where my question lies. Thanks in advance.

Code shared below:

from arcgis.gis import GIS
from IPython.display import display
import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from arcgis.features import FeatureLayer
import os
gis = GIS("home")

lyr_url = 'https://services6.arcgis.com/T8eS7sop5hLmgRRH/arcgis/rest/services/PublicProjectIdeas_ZS/FeatureServ...'
layer = FeatureLayer(lyr_url)
layer

feature_layer = projIdeas
feature_layer

query = feature_layer.query()

projIdeasTable = query
sdf = projIdeasTable.sdf
sdf.head()

projectIdeasTable = sdf.spatial.to_table('ProjectIdeas.csv')

 

 

0 Kudos
4 Replies
EarlMedina
Esri Regular Contributor

I'm going to assume from your usage of GIS("home") you are running this notebook in ArcGIS Pro?

I suspect the csv is being written, but perhaps not where you expect to find it. Add another cell and run this to see the current working directory:

!echo %cd%

 

Hope this helps!

0 Kudos
ZacharyStanley
New Contributor III

Hi @EarlMedina 

I'm running the notebook on AGOL and get the following results when running your command

```

/bin/bash: /opt/conda/lib/libtinfo.so.6: no version information available (required by /bin/bash)
%cd%
0 Kudos
ZacharyStanley
New Contributor III

If I set the arguments to my AGOL org account i.e. 

gis = GIS(url, username, password)

 

and use to_table method::

projectIdeasTable = sdf.spatial.to_table(name='ProjectIdeas.csv', location=gis)

 

I get this string error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [73], in <cell line: 1>()
----> 1 projectIdeasTable = sdf.spatial.to_table(name='ProjectIdeas.csv', location=gis)

File /opt/conda/lib/python3.9/site-packages/arcgis/features/geo/_accessor.py:2433, in GeoAccessor.to_table(self, location, overwrite, **kwargs)
   2431 origin_columns = self._data.columns.tolist()
   2432 origin_index = copy.deepcopy(self._data.index)
-> 2433 location = os.path.abspath(location)
   2434 table = run_and_hide(
   2435     to_table,
   2436     **{
   (...)
   2441     },
   2442 )
   2443 self._data.columns = origin_columns

File /opt/conda/lib/python3.9/posixpath.py:375, in abspath(path)
    373 def abspath(path):
    374     """Return an absolute path."""
--> 375     path = os.fspath(path)
    376     if not isabs(path):
    377         if isinstance(path, bytes):

TypeError: expected str, bytes or os.PathLike object, not GIS

 

0 Kudos
EarlMedina
Esri Regular Contributor

Oh, I see. I didn't catch that you were running this online. I believe you want to use that method locally if you want to save to a file to disk.

0 Kudos