<?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: Overwrite Hosted Table with new data in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1061866#M61242</link>
    <description>&lt;P&gt;Chris,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Saw your post over on&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/overwrite-hosted-table/m-p/1060280#M6100" target="_blank" rel="noopener"&gt;Overwrite Hosted Table&lt;/A&gt;&amp;nbsp;but it disappeared.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I replied over there since there was already a chain of conversation.&lt;/P&gt;</description>
    <pubDate>Wed, 26 May 2021 16:06:18 GMT</pubDate>
    <dc:creator>ConradSchaefer__DOIT_</dc:creator>
    <dc:date>2021-05-26T16:06:18Z</dc:date>
    <item>
      <title>Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1060818#M61216</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am having a challenging time trying to write a code using the ArcGIS Python API in Notebooks to update or overwrite an existing hosted table with new data obtained via a Survey123 form. The ultimate goal will be to set this to update automatically at regular intervals.&lt;/P&gt;&lt;P&gt;I have tried to resolve using similar previous posts&amp;nbsp;(e.g.&amp;nbsp;&lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/overwrite-hosted-table/m-p/1060280#M6100" target="_self"&gt;Overwrite Hosted Table&lt;/A&gt;), but to no avail.&lt;/P&gt;&lt;P&gt;My initial steps were to run my analysis code and add the output table as a CSV to my organisational AGOL account, which I then manually published as a Hosted Table. However, when I run the below steps, the output messages appears to be fine, but when I go to check the hosted table data in my 'Content', it has not updated. Code is in the image, below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ChrisMalam_0-1621814237789.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/13977iDBC3413C33CC40D5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ChrisMalam_0-1621814237789.png" alt="ChrisMalam_0-1621814237789.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;As additional context:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Given my analysis is all scripted, the hosted table name, properties and schema are remain the same.&lt;/LI&gt;&lt;LI&gt;Code runs ALL Survey123 data each time, rather than just those new records submitted since last run (there's are &amp;lt;1000 records).&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Any assistance would be much appreciated as I am still relatively new to Notebooks and python language. Happy to include additional information if needed.&lt;/P&gt;&lt;P&gt;Thanks!!&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 01:37:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1060818#M61216</guid>
      <dc:creator>ChrisMalam</dc:creator>
      <dc:date>2021-05-24T01:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1060821#M61217</link>
      <description>&lt;P&gt;Hopefully this will get smoother but for now there seem to be various issues with replacing an item. However, an alternative strategy we have had success with is looping through and deleting all rows and then writing all new records to the item with cursors.&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 02:10:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1060821#M61217</guid>
      <dc:creator>KevinMacLeodCAI</dc:creator>
      <dc:date>2021-05-24T02:10:17Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1060826#M61218</link>
      <description>&lt;P&gt;If you've got an input table, a target feature layer, no changes to the schema, &lt;EM&gt;and&lt;/EM&gt; you're working with the ArcGIS Python API, what you need is a &lt;A href="https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/" target="_self"&gt;spatially enabled dataframe&lt;/A&gt;. It does appear that you're working with a dataframe already in your screenshot, is that correct? Just call &lt;STRONG&gt;.spatial&lt;/STRONG&gt; on the dataframe to access the additional capabilities added in the ArcGIS Python API.&lt;/P&gt;&lt;P&gt;Try something like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;new_data = agg2[(agg2.Site == 'Callumbrae')]

rabbit = gis.content.get('your-itemid')

rabbit.tables[0].manager.truncate()

rabit.tables[0].edit_features(adds=new_data.spatial.to_featureset())&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 24 May 2021 02:46:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1060826#M61218</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-05-24T02:46:53Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1061004#M61223</link>
      <description>&lt;P&gt;I've also used the following successfully - my sources vary from CSV to Excel to Google Sheets, Smartsheets, etc.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;commRecTable = gis.content.get('{insert item ID here')
commRec_collection = FeatureLayerCollection.fromitem(commRecTable)
commRec_collection.manager.overwrite(commRecCSV)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; This one was created by using a spatially enabled data frame and manipulating my date fields, etc. using Pandas, then exporting it to a CSV.&amp;nbsp; I then overwrite and upload my new CSV every hour.&amp;nbsp; I do this for about 6 different items.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 15:53:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1061004#M61223</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2021-05-24T15:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1061866#M61242</link>
      <description>&lt;P&gt;Chris,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Saw your post over on&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/overwrite-hosted-table/m-p/1060280#M6100" target="_blank" rel="noopener"&gt;Overwrite Hosted Table&lt;/A&gt;&amp;nbsp;but it disappeared.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I replied over there since there was already a chain of conversation.&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 16:06:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1061866#M61242</guid>
      <dc:creator>ConradSchaefer__DOIT_</dc:creator>
      <dc:date>2021-05-26T16:06:18Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1062689#M61257</link>
      <description>&lt;P&gt;Hi Josh,&lt;/P&gt;&lt;P&gt;Thanks for the suggestion - when I tried running on my spatial dataframe I get the following error on the "rabbit.tables[0].manager.truncate()" command:&lt;/P&gt;&lt;P&gt;"IndexError: list index is out of range"&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 00:55:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1062689#M61257</guid>
      <dc:creator>ChrisMalam</dc:creator>
      <dc:date>2021-05-28T00:55:08Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1062704#M61258</link>
      <description>&lt;P&gt;Hm. Is it a feature layer with some geometry, and not a table? In that case, it would be rabbit.&lt;STRONG&gt;layers&lt;/STRONG&gt;[0] etc etc.&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 02:16:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1062704#M61258</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-05-28T02:16:08Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1099501#M62470</link>
      <description>&lt;P&gt;I am struggling with this same issue.&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I have tried using the FeatureLayerCollection.manager.overwrite() but that just appears to reread .sd files and restore all the original features without actually overwriting. Lots of posts having&amp;nbsp;the same issue. Using the processes described here I have gotten closer but not all the way.&amp;nbsp; The features all get added but with no attributes.&amp;nbsp; Not sure what I am missing but have struggled for too long on this.&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Here is my simple code:&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;This same query was used to to create a FGD feature class. I then published this using ArcGIS Pro as a hosted FeatureLayer&lt;BR /&gt;gis = GIS(url='&lt;A href="https://ArcGIS" target="_blank" rel="noopener"&gt;https://ArcGIS&lt;/A&gt; Enterprise Portal Server', username=portalUser, password=portalUserPass)&lt;BR /&gt;existingFS_item = gis.content.get("294c153f5ad64856b275b162eb78731b")&lt;BR /&gt;sql_query=pd.read_sql_query("""SELECT ....&lt;BR /&gt;******very long SQL query******* """)&lt;BR /&gt;sdf=pd.DataFrame.spatial.from_xy(df=sql_query,x_column='Longitude',y_column='Latitude',sr=4326)&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;existingFS_item.layers[0].manager.truncate()&lt;BR /&gt;test = existingFS_item.layers[0].edit_features(adds=sdf)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I have tried passing a single feature converted to a dictionary object &lt;/SPAN&gt;&lt;SPAN&gt;as well as writing this to a fileGDB and passing this in as an argument&amp;nbsp;&lt;SPAN&gt;GeoAccessor.from_featureclass(FileGDB\FeatureClass).&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;FONT face="inherit"&gt;All of these methods get the same result - all 500 &lt;/FONT&gt;records&lt;FONT face="inherit"&gt;&amp;nbsp;get created but no attributes get populated.&amp;nbsp; Any help would be appreciated.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;FONT face="inherit"&gt;Thanks.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 17 Sep 2021 18:44:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1099501#M62470</guid>
      <dc:creator>MarcusBodig</dc:creator>
      <dc:date>2021-09-17T18:44:25Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1106710#M62618</link>
      <description>&lt;P&gt;This worked like a charm and is so much simpler than most of other material out there on this topic. Thanks Kara!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Oct 2021 15:09:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1106710#M62618</guid>
      <dc:creator>DaveAlmond</dc:creator>
      <dc:date>2021-10-12T15:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1232712#M66100</link>
      <description>&lt;P&gt;I have tried this with a hosted table, however I am receiving the below error when running overwrite. Any thoughts?&lt;/P&gt;&lt;P&gt;ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().&lt;/P&gt;</description>
      <pubDate>Thu, 17 Nov 2022 15:01:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1232712#M66100</guid>
      <dc:creator>JoshObrecht1</dc:creator>
      <dc:date>2022-11-17T15:01:32Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1400500#M70156</link>
      <description>&lt;P&gt;Hi Kara, thanks for sharing this code! I've been trying to overwrite a hosted table in AGOL but I keep getting the following error:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;AttributeError&lt;/SPAN&gt;: 'NoneType' object has no attribute 'type'&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Have you ever encountered this. I've been reading other posts by&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/10527"&gt;@JakeSkinner&lt;/a&gt;&amp;nbsp;and trying to get this to work but no luck yet! Any thoughts anyone might have would be greatly appreciated. The hosted table is not spatial so I'm wondering if that has something to do with it. It's just 3 columns and a few rows of data that I'm extracted and using for a dashboard. Would love to be able to automate this!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 18:48:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1400500#M70156</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-03-25T18:48:47Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1400518#M70157</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/503780"&gt;@FredMitchell&lt;/a&gt;&amp;nbsp;can you share the service to an AGOL group and invite my account (&lt;STRONG&gt;jskinner_rats&lt;/STRONG&gt;)?&amp;nbsp; I can take a look to see if anything jumps out at me.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 19:08:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1400518#M70157</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2024-03-25T19:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1400606#M70163</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/10527"&gt;@JakeSkinner&lt;/a&gt;, I just invited you! Also, for context here is my python script minus our credentials:&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os, uuid
import pandas as pd
import datetime
from datetime import date, timedelta
from zipfile import ZipFile
from arcgis.features import GeoAccessor, GeoSeriesAccessor, FeatureLayerCollection
from arcgis.gis import GIS

# get table from AGOL
gis = GIS('https://kirklandwa.maps.arcgis.com', 'user', 'pw')
CrimeTrendsTable = gis.content.get('{78e4a92c5c324dd18cc6eee11cb6b2f7}')

# still trying to figure this part out whether it be an overwrite or a delete and append
CrimeTrendsTable.delete_features(where="1=1", return_delete_results=True)

# import feature class and create slice of the data set in a new dataframe
df = pd.DataFrame.spatial.from_featureclass(r"local path of larger feature class from sde")
df = df.sort_values(['OFFENSES_YEAR', 'CRIME_STAT_TYPE', 'FROM_DATE'], ascending= [True, True, True], ignore_index=True)
df = df.loc[:, ['OFFENSES_YEAR', 'CRIME_STAT_TYPE', 'FROM_DATE']]

# set variables for today and the previous 5 years
today = datetime.datetime.today()
todate1 = today - timedelta(365)
todate2 = today - timedelta(730)
todate3 = today - timedelta(1095)
todate4 = today - timedelta(1460)
todate5 = today - timedelta(1825)
this_year = datetime.datetime.today().year
one_year_ago = this_year - 1
two_years_ago = this_year - 2
three_years_ago = this_year - 3
four_years_ago = this_year - 4
five_years_ago = this_year - 5

# create new dataframes for current and each previous year to date
df0 = df.loc[(df['OFFENSES_YEAR']==this_year) &amp;amp; (df['FROM_DATE'] &amp;lt; today)]
df1 = df.loc[(df['OFFENSES_YEAR']==one_year_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate1)]
df2 = df.loc[(df['OFFENSES_YEAR']==two_years_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate2)]
df3 = df.loc[(df['OFFENSES_YEAR']==three_years_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate3)]
df4 = df.loc[(df['OFFENSES_YEAR']==four_years_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate4)]
df5 = df.loc[(df['OFFENSES_YEAR']==five_years_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate5)]

# create tables for each current and previous year grouped by crime type, add together previous year tables and average
table0 = df0.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count().reset_index()\
            .rename(columns={"OFFENSES_YEAR" : this_year})
table1 = df1.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
table2 = df2.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
table3 = df3.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
table4 = df4.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
table5 = df5.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
avgtable = pd.concat([table1, table2, table3, table4, table5]).groupby('CRIME_STAT_TYPE').mean().reset_index()\
             .rename(columns={"OFFENSES_YEAR" : "previous5yearAverage"})
trendstable = avgtable.join(table0.set_index('CRIME_STAT_TYPE'), on='CRIME_STAT_TYPE').fillna(0)
trendstable.to_csv(r"local path.csv", sep='\t', encoding='utf-8')

# hoping to add a line here that would update my hosted table from the trendstable dataframe...
trendstable.to_featurelayer('CrimeTrends')

print(trendstable)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 21:28:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1400606#M70163</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-03-25T21:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1400664#M70165</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/503780"&gt;@FredMitchell&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Try the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os, uuid
import pandas as pd
import datetime
from datetime import timedelta
from arcgis.features import GeoAccessor, FeatureLayer
from arcgis.gis import GIS

# Variables
url = 'https://kirklandwa.maps.arcgis.com'
username = 'user'
password = 'pw'
hostedTableID = '78e4a92c5c324dd18cc6eee11cb6b2f7'
outputCSVFile = r'local path to CSV'

# get table from AGOL
gis = GIS(url, username, password)
CrimeTrendsTable = gis.content.get(hostedTableID)
CrimeTrendsTableLyr = CrimeTrendsTable.tables[0]
crimeLayer = FeatureLayer(CrimeTrendsTableLyr.url, gis=gis)

# Truncate table
CrimeTrendsTableLyr.manager.truncate()

# import feature class and create slice of the data set in a new dataframe
df = pd.DataFrame.spatial.from_featureclass(r"local path of larger feature class from sde")
df = df.sort_values(['OFFENSES_YEAR', 'CRIME_STAT_TYPE', 'FROM_DATE'], ascending= [True, True, True], ignore_index=True)
df = df.loc[:, ['OFFENSES_YEAR', 'CRIME_STAT_TYPE', 'FROM_DATE']]

# set variables for today and the previous 5 years
today = datetime.datetime.today()
todate1 = today - timedelta(365)
todate2 = today - timedelta(730)
todate3 = today - timedelta(1095)
todate4 = today - timedelta(1460)
todate5 = today - timedelta(1825)
this_year = datetime.datetime.today().year
one_year_ago = this_year - 1
two_years_ago = this_year - 2
three_years_ago = this_year - 3
four_years_ago = this_year - 4
five_years_ago = this_year - 5

# create new dataframes for current and each previous year to date
df0 = df.loc[(df['OFFENSES_YEAR']==this_year) &amp;amp; (df['FROM_DATE'] &amp;lt; today)]
df1 = df.loc[(df['OFFENSES_YEAR']==one_year_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate1)]
df2 = df.loc[(df['OFFENSES_YEAR']==two_years_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate2)]
df3 = df.loc[(df['OFFENSES_YEAR']==three_years_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate3)]
df4 = df.loc[(df['OFFENSES_YEAR']==four_years_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate4)]
df5 = df.loc[(df['OFFENSES_YEAR']==five_years_ago) &amp;amp; (df['FROM_DATE'] &amp;lt; todate5)]

# create tables for each current and previous year grouped by crime type, add together previous year tables and average
table0 = df0.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count().reset_index()\
            .rename(columns={"OFFENSES_YEAR" : this_year})
table1 = df1.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
table2 = df2.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
table3 = df3.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
table4 = df4.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
table5 = df5.groupby('CRIME_STAT_TYPE')['OFFENSES_YEAR'].count()
avgtable = pd.concat([table1, table2, table3, table4, table5]).groupby('CRIME_STAT_TYPE').mean().reset_index()\
             .rename(columns={"OFFENSES_YEAR" : "previous5yearAverage"})
trendstable = avgtable.join(table0.set_index('CRIME_STAT_TYPE'), on='CRIME_STAT_TYPE').fillna(0)
trendstable.to_csv(outputCSVFile, sep='\t', encoding='utf-8')

# update hosted table from csv file
csvDF = GeoAccessor.from_table(outputCSVFile)
adds_fs = csvDF.spatial.to_featureset()
adds_dict = adds_fs.to_dict()
adds = adds_dict["features"]
crimeLayer.edit_features(adds=adds)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 23:24:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1400664#M70165</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2024-03-25T23:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1401200#M70182</link>
      <description>&lt;P&gt;Hi Jake,&lt;/P&gt;&lt;P&gt;Thanks for looking into this for me and adding these key pieces of code. When I run line 66 I get the following error:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;---------------------------------------------------------------------------&lt;/SPAN&gt;
&lt;SPAN class=""&gt;KeyError&lt;/SPAN&gt;                                  Traceback (most recent call last)
In  &lt;SPAN class=""&gt;[50]&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;1&lt;/SPAN&gt;:     adds_fs = csvDF.spatial.to_featureset()

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;to_featureset&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;3573&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;return&lt;/SPAN&gt; FeatureSet.from_dict(&lt;SPAN class=""&gt;self&lt;/SPAN&gt;.__feature_set__)

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;__feature_set__&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;3297&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;if&lt;/SPAN&gt; &lt;SPAN class=""&gt;self&lt;/SPAN&gt;.sr &lt;SPAN class=""&gt;is&lt;/SPAN&gt; &lt;SPAN class=""&gt;None&lt;/SPAN&gt;:

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;sr&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;3503&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;for&lt;/SPAN&gt; g &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;self&lt;/SPAN&gt;._data[&lt;SPAN class=""&gt;self&lt;/SPAN&gt;.name]

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\frame.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;__getitem__&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;3505&lt;/SPAN&gt;:  indexer = &lt;SPAN class=""&gt;self&lt;/SPAN&gt;.columns.get_loc(key)

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\indexes\base.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;get_loc&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;3631&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;raise&lt;/SPAN&gt; &lt;SPAN class=""&gt;KeyError&lt;/SPAN&gt;(key) &lt;SPAN class=""&gt;from&lt;/SPAN&gt; &lt;SPAN class=""&gt;err&lt;/SPAN&gt;

&lt;SPAN class=""&gt;KeyError&lt;/SPAN&gt;: None
&lt;SPAN class=""&gt;---------------------------------------------------------------------------&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;P&gt;Any thoughts?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 22:11:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1401200#M70182</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-03-26T22:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1401362#M70186</link>
      <description>&lt;P&gt;Can you share the CSV you're using to one of the AGOL groups you invited me to?&lt;/P&gt;</description>
      <pubDate>Wed, 27 Mar 2024 11:30:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1401362#M70186</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2024-03-27T11:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1401545#M70193</link>
      <description>&lt;P&gt;It's shared!&lt;/P&gt;</description>
      <pubDate>Wed, 27 Mar 2024 16:27:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1401545#M70193</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-03-27T16:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1401871#M70201</link>
      <description>&lt;P&gt;It's something with how the CSV is being created.&amp;nbsp; When trying to read the CSV file, it is not being read correctly.&amp;nbsp; See below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JakeSkinner_0-1711622478796.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/99306i5570AD6BF8F99510/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JakeSkinner_0-1711622478796.png" alt="JakeSkinner_0-1711622478796.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm not sure if it has to do with the delimiter being used or something else, but this is where you will need to troubleshoot further.&amp;nbsp; &amp;nbsp;Sorry, I'm not an expert with dataframes, so not sure how much help I will be.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 10:42:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1401871#M70201</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2024-03-28T10:42:11Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1404670#M70242</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/10527"&gt;@JakeSkinner&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Thanks for getting me this far! I ended up making a new post here which I think you may have already noticed from your kudos:&amp;nbsp;&lt;A href="https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1404097#M70234" target="_blank" rel="noopener"&gt;https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1404097#M70234&lt;/A&gt;&amp;nbsp;- just wanted to post here too and close the loop unless anyone else is having a similar issue. I'm not sure if the issue was with the delimiter, separator, or header or something else in how CSVs are created and read back in. During my research I found some weird behavior with the CSV. For example, when I opened it in Excel all the data would appear fine. Same for if I viewed it using the print command in Python. However, when trying to read it in using a command or open it as a table in ArcGIS Pro some columns would have null data. Using XLS instead worked great so I'm glad to have a solution but would like to understand the cause better at some point. Thanks again for all your help!&lt;/P&gt;</description>
      <pubDate>Tue, 02 Apr 2024 15:43:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1404670#M70242</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-04-02T15:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite Hosted Table with new data</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1548797#M73030</link>
      <description>&lt;P&gt;Thank you!&amp;nbsp; The FeatureCollection suggested method below didn't work for my nonspatial Hosted Table, but this did. Thanks for posting it!!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2024 18:55:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-hosted-table-with-new-data/m-p/1548797#M73030</guid>
      <dc:creator>CherieBryant</dc:creator>
      <dc:date>2024-10-15T18:55:56Z</dc:date>
    </item>
  </channel>
</rss>

