<?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: Export dataframe to table in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019352#M5466</link>
    <description>&lt;P&gt;When you're working with &lt;STRONG&gt;pandas&lt;/STRONG&gt; and &lt;STRONG&gt;arcgis&lt;/STRONG&gt; together, you get the added functionality of the spatial property of your dataframes. I know you said it's a non-spatial table, but I mean the literal&amp;nbsp;&lt;STRONG&gt;your_dataframe.spatial&lt;/STRONG&gt; type. There are several options for exporting a dataframe that way, one of them being &lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.GeoAccessor.to_featurelayer" target="_self"&gt;&lt;STRONG&gt;to_featurelayer()&lt;/STRONG&gt;&lt;/A&gt;, which exports the results to a layer in the portal.&lt;/P&gt;&lt;P&gt;Alternatively, you can use one of the other options like &lt;STRONG&gt;to_table()&lt;/STRONG&gt; or &lt;STRONG&gt;to_featureclass()&lt;/STRONG&gt; to write the layer to file, which users could download from the &lt;STRONG&gt;Files&lt;/STRONG&gt; tab of the notebook viewer.&lt;/P&gt;&lt;P&gt;Lastly, as you did mention that it's a non-spatial table, you could just use the dataframe's own &lt;STRONG&gt;to_csv()&lt;/STRONG&gt; function to export the frame to a standalone file.&lt;/P&gt;</description>
    <pubDate>Sat, 23 Jan 2021 03:06:33 GMT</pubDate>
    <dc:creator>jcarlson</dc:creator>
    <dc:date>2021-01-23T03:06:33Z</dc:date>
    <item>
      <title>Export dataframe to table</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019327#M5465</link>
      <description>&lt;P&gt;I have written a python notebook in ArcGIS Online.&amp;nbsp; It reads an existing feature layer, queries it and calculates new fields, then creates a summary table using pandas groupby.&amp;nbsp; I want to make this table available to users.&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I export a non-spatial pandas dataframe to a feature layer/table that an AGOL user can download.&amp;nbsp; Or a similar solution.&amp;nbsp; Right now, a user would have to run the notebook and copy the displayed summary table to clipboard.&amp;nbsp; The users are not comfortable with code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 23:51:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019327#M5465</guid>
      <dc:creator>KevinMayall</dc:creator>
      <dc:date>2021-01-22T23:51:28Z</dc:date>
    </item>
    <item>
      <title>Re: Export dataframe to table</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019352#M5466</link>
      <description>&lt;P&gt;When you're working with &lt;STRONG&gt;pandas&lt;/STRONG&gt; and &lt;STRONG&gt;arcgis&lt;/STRONG&gt; together, you get the added functionality of the spatial property of your dataframes. I know you said it's a non-spatial table, but I mean the literal&amp;nbsp;&lt;STRONG&gt;your_dataframe.spatial&lt;/STRONG&gt; type. There are several options for exporting a dataframe that way, one of them being &lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.GeoAccessor.to_featurelayer" target="_self"&gt;&lt;STRONG&gt;to_featurelayer()&lt;/STRONG&gt;&lt;/A&gt;, which exports the results to a layer in the portal.&lt;/P&gt;&lt;P&gt;Alternatively, you can use one of the other options like &lt;STRONG&gt;to_table()&lt;/STRONG&gt; or &lt;STRONG&gt;to_featureclass()&lt;/STRONG&gt; to write the layer to file, which users could download from the &lt;STRONG&gt;Files&lt;/STRONG&gt; tab of the notebook viewer.&lt;/P&gt;&lt;P&gt;Lastly, as you did mention that it's a non-spatial table, you could just use the dataframe's own &lt;STRONG&gt;to_csv()&lt;/STRONG&gt; function to export the frame to a standalone file.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 03:06:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019352#M5466</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-01-23T03:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: Export dataframe to table</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019391#M5467</link>
      <description>&lt;P&gt;Thanks for your help.&amp;nbsp; to_featurelayer would be ideal and I could share it to allow the user to export the data from an item page.&lt;/P&gt;&lt;P&gt;I whittled down my code to a bare minimum and found that even with a simple SDF from an existing feature layer, I could not save it back out with to_featurelayer.&lt;/P&gt;&lt;P&gt;AttributeError: 'DataFrame' object has no attribute 'to_featurelayer'&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
import pandas as pd
import numpy as np
gis = GIS("home")
# Feature service of street lights (read-only)
lights = gis.content.get("c6f328cfa65846308881279581c681f6")
lyr = lights.layers[0]
# Make a SDF directly from a feature layer
lyr_sdf = pd.DataFrame.spatial.from_layer(lyr)
# Save back out as a feature layer
lyr_sdf.to_featurelayer('my_test_lyr')&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&amp;lt;ipython-input-60-813286c974b1&amp;gt; in &amp;lt;module&amp;gt;
      1 # Save back out as a feature layer
----&amp;gt; 2 lyr_sdf.to_featurelayer('my_test_lyr')

/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5273                 return self[name]
-&amp;gt; 5274             return object.__getattribute__(self, name)
   5275 
   5276     def __setattr__(self, name: str, value) -&amp;gt; None:

AttributeError: 'DataFrame' object has no attribute 'to_featurelayer'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 13:05:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019391#M5467</guid>
      <dc:creator>KevinMayall</dc:creator>
      <dc:date>2021-01-23T13:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: Export dataframe to table</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019396#M5468</link>
      <description>&lt;P&gt;You've got to use the spatial type: "&lt;FONT face="courier new,courier"&gt;lyr_sdf.&lt;STRONG&gt;spatial&lt;/STRONG&gt;.to_featurelayer('my_test_lyr')&lt;/FONT&gt;"&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jan 2021 14:54:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019396#M5468</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-01-23T14:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Export dataframe to table</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019513#M5469</link>
      <description>&lt;P&gt;Thank you Josh!&amp;nbsp; So I am one step closer, in that the "to_featurelayer" is now saving my feature layer back out to a feature layer.&amp;nbsp; Back in the original post, I create a summary table with groupby and tried to apply to_featurelayer to that table, but it gives an error.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
import pandas as pd
import numpy as np
gis = GIS("home")
# Feature service of street lights (read-only)
lights = gis.content.get("c6f328cfa65846308881279581c681f6")
lyr = lights.layers[0]
result = lyr.query(where="IsMetered='No' AND NumLights&amp;gt;0 AND BulbWattage&amp;gt;0",out_fields='LocationType,BulbWattage')
result_sdf = result.sdf
# Test: Save back out as a feature layer
result_sdf.spatial.to_featurelayer('result_fl')  # this works
# Create a summary table
summary = result_sdf.groupby(by=['LocationType','BulbWattage'],as_index=False).count()
summary = summary.drop('SHAPE', axis=1).rename(columns={"OBJECTID": "count"})
summary&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;	LocationType	BulbWattage	count
0	Park	50	4
1	Park	68	2
2	Park	250	4
3	Parking Lot	250	2
4	Street	15	9
5	Street	22	34
6	Street	50	83
7	Street	54	8
8	Street	67	2
9	Street	68	141
10	Street	87	11
11	Street	175	1
12	Street	250	69&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;summary.spatial.to_featurelayer(title='summary_table_fl',gis=gis)&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&amp;lt;ipython-input-11-8da99776fdd8&amp;gt; in &amp;lt;module&amp;gt;
----&amp;gt; 1 summary.spatial.to_featurelayer(title='summary_table_fl',gis=gis)

/opt/conda/lib/python3.6/site-packages/arcgis/features/geo/_accessor.py in to_featurelayer(self, title, gis, tags, folder)
   2132                 raise ValueError("GIS object must be provided")
   2133         content = gis.content
-&amp;gt; 2134         return content.import_data(self._data, folder=folder, title=title, tags=tags)
   2135     # ----------------------------------------------------------------------
   2136     @staticmethod

/opt/conda/lib/python3.6/site-packages/arcgis/gis/__init__.py in import_data(self, df, address_fields, folder, item_id, **kwargs)
   4913                     "sourceCountry":"",
   4914                     "sourceCountryHint":"",
-&amp;gt; 4915                     "geocodeServiceUrl":self._gis.properties.helperServices.geocode[0]['url']
   4916                 }
   4917             }

IndexError: list index out of range&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems like the code is trying to automatically geocode the table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2021 01:24:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019513#M5469</guid>
      <dc:creator>KevinMayall</dc:creator>
      <dc:date>2021-01-25T01:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: Export dataframe to table</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019522#M5470</link>
      <description>&lt;P&gt;Ah, that's right. The function tries to create a new &lt;EM&gt;spatial&lt;/EM&gt; feature class, and if there's no geometry column, it'll try to infer it from some text column.&lt;/P&gt;&lt;P&gt;It's worth nothing that a &lt;STRONG&gt;Table&lt;/STRONG&gt; is a distinct class of its own in the arcgis python API. I don't think there's a way to publish it directly to a hosted table, at least not at the moment. There seems to be a &lt;STRONG&gt;to_table&lt;/STRONG&gt; function, but a few tests of it haven't yielded anything helpful.&lt;/P&gt;&lt;P&gt;A possible solution I considered, but have not tested:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Export the dataframe to a text file using &lt;STRONG&gt;pandas.DataFrame.to_csv()&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;Add the CSV as a content item using &lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.add" target="_self"&gt;&lt;STRONG&gt;arcgis.gis.ContentManager.add()&lt;/STRONG&gt;&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Use &lt;STRONG&gt;&lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.publish" target="_self"&gt;arcgis.gis.Item.publish()&lt;/A&gt;&lt;/STRONG&gt; to publish the newly-added CSV as a hosted service&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;You may also want to look at &lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.import_data" target="_self"&gt;&lt;STRONG&gt;arcgis.gis.ContentManager.import_data()&lt;/STRONG&gt;&lt;/A&gt;, as it seems to allow the direct upload / publishing of CSV files as well, and doesn't require location fields to be present. I saw that that function accepts dataframes as well, but if I'm reading it right, would require that the dataframe be spatial, or at least have location fields, which sounds similar to what's happening to you with the &lt;STRONG&gt;to_featureclass()&lt;/STRONG&gt; attempt.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2021 03:50:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019522#M5470</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-01-25T03:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: Export dataframe to table</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019652#M5472</link>
      <description>&lt;P&gt;Thanks again, Josh.&amp;nbsp; After some testing ...&amp;nbsp;&lt;/P&gt;&lt;P&gt;(1) I agree that there does not seem to be a way to publish to a hosted table at the moment.&amp;nbsp; Not surprising as I think Dashboards only recently allowed a hosted table as a data source.&amp;nbsp; &lt;STRONG&gt;to_table()&lt;/STRONG&gt; seems to be designed for outputting to a gdb table.&lt;/P&gt;&lt;P&gt;(2) I tried adding the CSV and publishing to an item as you suggested.&amp;nbsp; The result of that item is, in fact, a hosted table.&amp;nbsp; So this most directly addresses my original post.&amp;nbsp; From the item, a user can download in multiple formats.&amp;nbsp; If the user ends up downloading in CSV, then the publishing step in the code isn't even necessary.&lt;/P&gt;&lt;P&gt;(3) I interpret the documentation for &lt;STRONG&gt;import_data()&lt;/STRONG&gt; as you do - that a spatial dataframe is required.&amp;nbsp; I didn't even bother to try this.&lt;/P&gt;&lt;P&gt;(4) One other option I came up with was to add a dummy geometry to the summary table to force it into a spatial dataframe.&amp;nbsp; I added 'X' and 'Y' fields and set them to zero.&amp;nbsp; Then I created a new spatial dataframe with &lt;STRONG&gt;pd.DataFrame.spatial.from_xy&lt;/STRONG&gt;(df, xfield, yfield, spatialReference).&amp;nbsp; I was then able to save it to a feature layer and afterwards drop the X and Y fields from the layer.&amp;nbsp; However, this wasn't as clean as point #2 above.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jan 2021 15:15:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-dataframe-to-table/m-p/1019652#M5472</guid>
      <dc:creator>KevinMayall</dc:creator>
      <dc:date>2021-01-25T15:15:12Z</dc:date>
    </item>
  </channel>
</rss>

