<?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 Overwriting hosted table in ArcGIS Online with CSV file from Pandas dataframe in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1403884#M70224</link>
    <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;SPAN&gt;I have a script that I'm using to sort through a very large feature layer with thousands of records and create a summary table of them by type. It also calculates the count of records of the last 5 years to the current date for each year rather than the entire calendar year and then calculates the average. My output table is the type, average, and current count for the year. Right now I have this outputting to a CSV but I'm trying to get that CSV to overwrite a hosted feature table. I've referenced some posts on the Esri Community forums and it seems like this should be possible but I keep getting an error. Here is my code:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;import&lt;/SPAN&gt; arcpy, os, uuid
&lt;SPAN class=""&gt;import&lt;/SPAN&gt; pandas &lt;SPAN class=""&gt;as&lt;/SPAN&gt; pd
&lt;SPAN class=""&gt;import&lt;/SPAN&gt; datetime
&lt;SPAN class=""&gt;from&lt;/SPAN&gt; datetime &lt;SPAN class=""&gt;import&lt;/SPAN&gt; timedelta
&lt;SPAN class=""&gt;from&lt;/SPAN&gt; arcgis.features &lt;SPAN class=""&gt;import&lt;/SPAN&gt; GeoAccessor, FeatureLayer
&lt;SPAN class=""&gt;from&lt;/SPAN&gt; arcgis.gis &lt;SPAN class=""&gt;import&lt;/SPAN&gt; GIS

&lt;SPAN class=""&gt;# variables&lt;/SPAN&gt;

url = &lt;SPAN class=""&gt;'arcgis online url'&lt;/SPAN&gt;
username = &lt;SPAN class=""&gt;'username'&lt;/SPAN&gt;
password = &lt;SPAN class=""&gt;'password'&lt;/SPAN&gt;
hostedTableID = &lt;SPAN class=""&gt;'item ID'&lt;/SPAN&gt;
inputfc = &lt;SPAN class=""&gt;r'feature layer from sde on server'&lt;/SPAN&gt;
outputCSVFile = &lt;SPAN class=""&gt;r'csv in folder on server'&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# get table from AGOL&lt;/SPAN&gt;
gis = GIS(url, username, password)
CrimeTrendsTable = gis.content.get(hostedTableID)
CrimeTrendsTableLyr = CrimeTrendsTable.tables[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]
crimeLayer = FeatureLayer(CrimeTrendsTableLyr.url, gis=gis)

&lt;SPAN class=""&gt;# truncate table&lt;/SPAN&gt;
crimeLayer.manager.truncate()

&lt;SPAN class=""&gt;# import feature class and create slice of the data set in a new dataframe&lt;/SPAN&gt;
df = pd.DataFrame.spatial.from_featureclass(inputfc)
df = df.sort_values([&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;], ascending= [&lt;SPAN class=""&gt;True&lt;/SPAN&gt;, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;], ignore_index=&lt;SPAN class=""&gt;True&lt;/SPAN&gt;)
df = df.loc[:, [&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;]]

&lt;SPAN class=""&gt;# set variables for today and the previous 5 years&lt;/SPAN&gt;
today = datetime.datetime.today()
todate1 = today - timedelta(&lt;SPAN class=""&gt;365&lt;/SPAN&gt;)
todate2 = today - timedelta(&lt;SPAN class=""&gt;730&lt;/SPAN&gt;)
todate3 = today - timedelta(&lt;SPAN class=""&gt;1095&lt;/SPAN&gt;)
todate4 = today - timedelta(&lt;SPAN class=""&gt;1460&lt;/SPAN&gt;)
todate5 = today - timedelta(&lt;SPAN class=""&gt;1825&lt;/SPAN&gt;)
this_year = datetime.datetime.today().year
one_year_ago = this_year - &lt;SPAN class=""&gt;1&lt;/SPAN&gt;
two_years_ago = this_year - &lt;SPAN class=""&gt;2&lt;/SPAN&gt;
three_years_ago = this_year - &lt;SPAN class=""&gt;3&lt;/SPAN&gt;
four_years_ago = this_year - &lt;SPAN class=""&gt;4&lt;/SPAN&gt;
five_years_ago = this_year - &lt;SPAN class=""&gt;5&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# create new dataframes for current and each previous year to date&lt;/SPAN&gt;
df0 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==this_year) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; today)]
df1 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==one_year_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate1)]
df2 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==two_years_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate2)]
df3 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==three_years_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate3)]
df4 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==four_years_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate4)]
df5 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==five_years_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate5)]

&lt;SPAN class=""&gt;# create tables for each current and previous year grouped by crime type, add together previous year tables and average&lt;/SPAN&gt;
table0 = df0.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count().reset_index()\
            .rename(columns={&lt;SPAN class=""&gt;"OFFENSES_YEAR"&lt;/SPAN&gt; : this_year})
table1 = df1.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
table2 = df2.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
table3 = df3.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
table4 = df4.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
table5 = df5.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
avgtable = pd.concat([table1, table2, table3, table4, table5]).groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;).mean().reset_index()\
             .rename(columns={&lt;SPAN class=""&gt;"OFFENSES_YEAR"&lt;/SPAN&gt; : &lt;SPAN class=""&gt;"previous5yearAverage"&lt;/SPAN&gt;})
trendstable = avgtable.join(table0.set_index(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;), on=&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;).fillna(&lt;SPAN class=""&gt;0&lt;/SPAN&gt;)
trendstable.to_csv(outputCSVFile, sep=&lt;SPAN class=""&gt;'\t'&lt;/SPAN&gt;, encoding=&lt;SPAN class=""&gt;'utf-8'&lt;/SPAN&gt;)

&lt;SPAN class=""&gt;# update hosted table from csv file&lt;/SPAN&gt;
csvDF = GeoAccessor.from_table(outputCSVFile)
adds_fs = csvDF.spatial.to_featureset()
adds_dict = adds_fs.to_dict()
adds = adds_dict[&lt;SPAN class=""&gt;"features"&lt;/SPAN&gt;]
crimeLayer.edit_features(adds=adds)&lt;/PRE&gt;&lt;P&gt;Here's an example of some data from my csv table:&lt;/P&gt;&lt;DIV class=""&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;CRIME_STAT_TYPE&lt;/TD&gt;&lt;TD&gt;previous5yearAverage&lt;/TD&gt;&lt;TD&gt;2024&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;AGGRAVATED ASSAULT&lt;/TD&gt;&lt;TD&gt;12.000000&lt;/TD&gt;&lt;TD&gt;9.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;ALL OTHER OFFENSES&lt;/TD&gt;&lt;TD&gt;54.800000&lt;/TD&gt;&lt;TD&gt;46.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;ANIMAL CRUELTY&lt;/TD&gt;&lt;TD&gt;1.666667&lt;/TD&gt;&lt;TD&gt;1.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;ARSON&lt;/TD&gt;&lt;TD&gt;1.000000&lt;/TD&gt;&lt;TD&gt;0.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;BAD CHECKS&lt;/TD&gt;&lt;TD&gt;1.750000&lt;/TD&gt;&lt;TD&gt;1.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;BURGLARY/BREAKING AND ENTERING&lt;/TD&gt;&lt;TD&gt;44.400000&lt;/TD&gt;&lt;TD&gt;50.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;TD&gt;WIRE FRAUD&lt;/TD&gt;&lt;TD&gt;6.000000&lt;/TD&gt;&lt;TD&gt;0.0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;P&gt;This is the error I'm getting when I run the adds_fs = csvDF.spatial.to_featureset() line:&lt;/P&gt;&lt;PRE&gt;---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
In  [&lt;SPAN class=""&gt;60&lt;/SPAN&gt;]:
Line &lt;SPAN class=""&gt;1&lt;/SPAN&gt;:     adds_fs = csvDF.spatial.to_featureset()

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; to_featureset:
Line &lt;SPAN class=""&gt;3573&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;return&lt;/SPAN&gt; FeatureSet.from_dict(self.__feature_set__)

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; __feature_set__:
Line &lt;SPAN class=""&gt;3297&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;if&lt;/SPAN&gt; self.sr &lt;SPAN class=""&gt;is&lt;/SPAN&gt; &lt;SPAN class=""&gt;None&lt;/SPAN&gt;:

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; sr:
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; self._data[self.name]

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\frame.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; __getitem__:
Line &lt;SPAN class=""&gt;3505&lt;/SPAN&gt;:  indexer = self.columns.get_loc(key)

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\indexes\base.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; get_loc:
Line &lt;SPAN class=""&gt;3631&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;raise&lt;/SPAN&gt; KeyError(key) &lt;SPAN class=""&gt;from&lt;/SPAN&gt; err

KeyError: &lt;SPAN class=""&gt;None&lt;/SPAN&gt;
---------------------------------------------------------------------------&lt;/PRE&gt;&lt;P&gt;Also here is a screenshot of the CSV. I think something is happening with how pandas is creating the CSV. When trying to read the CSV file, it is not being read correctly. 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: 599px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/99638i6A03CEF76B8B668A/image-size/large?v=v2&amp;amp;px=999" role="button" title="JakeSkinner_0-1711622478796.png" alt="JakeSkinner_0-1711622478796.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm wondering if it has something to do with the delimiter being used or something else?&lt;/P&gt;&lt;P&gt;I'm very new to all of this so any help I could get would be greatly appreciated.&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Mon, 01 Apr 2024 15:53:39 GMT</pubDate>
    <dc:creator>FredMitchell</dc:creator>
    <dc:date>2024-04-01T15:53:39Z</dc:date>
    <item>
      <title>Overwriting hosted table in ArcGIS Online with CSV file from Pandas dataframe in Python</title>
      <link>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1403884#M70224</link>
      <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;SPAN&gt;I have a script that I'm using to sort through a very large feature layer with thousands of records and create a summary table of them by type. It also calculates the count of records of the last 5 years to the current date for each year rather than the entire calendar year and then calculates the average. My output table is the type, average, and current count for the year. Right now I have this outputting to a CSV but I'm trying to get that CSV to overwrite a hosted feature table. I've referenced some posts on the Esri Community forums and it seems like this should be possible but I keep getting an error. Here is my code:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;import&lt;/SPAN&gt; arcpy, os, uuid
&lt;SPAN class=""&gt;import&lt;/SPAN&gt; pandas &lt;SPAN class=""&gt;as&lt;/SPAN&gt; pd
&lt;SPAN class=""&gt;import&lt;/SPAN&gt; datetime
&lt;SPAN class=""&gt;from&lt;/SPAN&gt; datetime &lt;SPAN class=""&gt;import&lt;/SPAN&gt; timedelta
&lt;SPAN class=""&gt;from&lt;/SPAN&gt; arcgis.features &lt;SPAN class=""&gt;import&lt;/SPAN&gt; GeoAccessor, FeatureLayer
&lt;SPAN class=""&gt;from&lt;/SPAN&gt; arcgis.gis &lt;SPAN class=""&gt;import&lt;/SPAN&gt; GIS

&lt;SPAN class=""&gt;# variables&lt;/SPAN&gt;

url = &lt;SPAN class=""&gt;'arcgis online url'&lt;/SPAN&gt;
username = &lt;SPAN class=""&gt;'username'&lt;/SPAN&gt;
password = &lt;SPAN class=""&gt;'password'&lt;/SPAN&gt;
hostedTableID = &lt;SPAN class=""&gt;'item ID'&lt;/SPAN&gt;
inputfc = &lt;SPAN class=""&gt;r'feature layer from sde on server'&lt;/SPAN&gt;
outputCSVFile = &lt;SPAN class=""&gt;r'csv in folder on server'&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# get table from AGOL&lt;/SPAN&gt;
gis = GIS(url, username, password)
CrimeTrendsTable = gis.content.get(hostedTableID)
CrimeTrendsTableLyr = CrimeTrendsTable.tables[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]
crimeLayer = FeatureLayer(CrimeTrendsTableLyr.url, gis=gis)

&lt;SPAN class=""&gt;# truncate table&lt;/SPAN&gt;
crimeLayer.manager.truncate()

&lt;SPAN class=""&gt;# import feature class and create slice of the data set in a new dataframe&lt;/SPAN&gt;
df = pd.DataFrame.spatial.from_featureclass(inputfc)
df = df.sort_values([&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;], ascending= [&lt;SPAN class=""&gt;True&lt;/SPAN&gt;, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;, &lt;SPAN class=""&gt;True&lt;/SPAN&gt;], ignore_index=&lt;SPAN class=""&gt;True&lt;/SPAN&gt;)
df = df.loc[:, [&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;, &lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;]]

&lt;SPAN class=""&gt;# set variables for today and the previous 5 years&lt;/SPAN&gt;
today = datetime.datetime.today()
todate1 = today - timedelta(&lt;SPAN class=""&gt;365&lt;/SPAN&gt;)
todate2 = today - timedelta(&lt;SPAN class=""&gt;730&lt;/SPAN&gt;)
todate3 = today - timedelta(&lt;SPAN class=""&gt;1095&lt;/SPAN&gt;)
todate4 = today - timedelta(&lt;SPAN class=""&gt;1460&lt;/SPAN&gt;)
todate5 = today - timedelta(&lt;SPAN class=""&gt;1825&lt;/SPAN&gt;)
this_year = datetime.datetime.today().year
one_year_ago = this_year - &lt;SPAN class=""&gt;1&lt;/SPAN&gt;
two_years_ago = this_year - &lt;SPAN class=""&gt;2&lt;/SPAN&gt;
three_years_ago = this_year - &lt;SPAN class=""&gt;3&lt;/SPAN&gt;
four_years_ago = this_year - &lt;SPAN class=""&gt;4&lt;/SPAN&gt;
five_years_ago = this_year - &lt;SPAN class=""&gt;5&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# create new dataframes for current and each previous year to date&lt;/SPAN&gt;
df0 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==this_year) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; today)]
df1 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==one_year_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate1)]
df2 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==two_years_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate2)]
df3 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==three_years_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate3)]
df4 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==four_years_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate4)]
df5 = df.loc[(df[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;]==five_years_ago) &amp;amp; (df[&lt;SPAN class=""&gt;'FROM_DATE'&lt;/SPAN&gt;] &amp;lt; todate5)]

&lt;SPAN class=""&gt;# create tables for each current and previous year grouped by crime type, add together previous year tables and average&lt;/SPAN&gt;
table0 = df0.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count().reset_index()\
            .rename(columns={&lt;SPAN class=""&gt;"OFFENSES_YEAR"&lt;/SPAN&gt; : this_year})
table1 = df1.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
table2 = df2.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
table3 = df3.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
table4 = df4.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
table5 = df5.groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;)[&lt;SPAN class=""&gt;'OFFENSES_YEAR'&lt;/SPAN&gt;].count()
avgtable = pd.concat([table1, table2, table3, table4, table5]).groupby(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;).mean().reset_index()\
             .rename(columns={&lt;SPAN class=""&gt;"OFFENSES_YEAR"&lt;/SPAN&gt; : &lt;SPAN class=""&gt;"previous5yearAverage"&lt;/SPAN&gt;})
trendstable = avgtable.join(table0.set_index(&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;), on=&lt;SPAN class=""&gt;'CRIME_STAT_TYPE'&lt;/SPAN&gt;).fillna(&lt;SPAN class=""&gt;0&lt;/SPAN&gt;)
trendstable.to_csv(outputCSVFile, sep=&lt;SPAN class=""&gt;'\t'&lt;/SPAN&gt;, encoding=&lt;SPAN class=""&gt;'utf-8'&lt;/SPAN&gt;)

&lt;SPAN class=""&gt;# update hosted table from csv file&lt;/SPAN&gt;
csvDF = GeoAccessor.from_table(outputCSVFile)
adds_fs = csvDF.spatial.to_featureset()
adds_dict = adds_fs.to_dict()
adds = adds_dict[&lt;SPAN class=""&gt;"features"&lt;/SPAN&gt;]
crimeLayer.edit_features(adds=adds)&lt;/PRE&gt;&lt;P&gt;Here's an example of some data from my csv table:&lt;/P&gt;&lt;DIV class=""&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;CRIME_STAT_TYPE&lt;/TD&gt;&lt;TD&gt;previous5yearAverage&lt;/TD&gt;&lt;TD&gt;2024&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;AGGRAVATED ASSAULT&lt;/TD&gt;&lt;TD&gt;12.000000&lt;/TD&gt;&lt;TD&gt;9.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;ALL OTHER OFFENSES&lt;/TD&gt;&lt;TD&gt;54.800000&lt;/TD&gt;&lt;TD&gt;46.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;ANIMAL CRUELTY&lt;/TD&gt;&lt;TD&gt;1.666667&lt;/TD&gt;&lt;TD&gt;1.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;ARSON&lt;/TD&gt;&lt;TD&gt;1.000000&lt;/TD&gt;&lt;TD&gt;0.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;BAD CHECKS&lt;/TD&gt;&lt;TD&gt;1.750000&lt;/TD&gt;&lt;TD&gt;1.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;BURGLARY/BREAKING AND ENTERING&lt;/TD&gt;&lt;TD&gt;44.400000&lt;/TD&gt;&lt;TD&gt;50.0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;TD&gt;WIRE FRAUD&lt;/TD&gt;&lt;TD&gt;6.000000&lt;/TD&gt;&lt;TD&gt;0.0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;P&gt;This is the error I'm getting when I run the adds_fs = csvDF.spatial.to_featureset() line:&lt;/P&gt;&lt;PRE&gt;---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
In  [&lt;SPAN class=""&gt;60&lt;/SPAN&gt;]:
Line &lt;SPAN class=""&gt;1&lt;/SPAN&gt;:     adds_fs = csvDF.spatial.to_featureset()

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; to_featureset:
Line &lt;SPAN class=""&gt;3573&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;return&lt;/SPAN&gt; FeatureSet.from_dict(self.__feature_set__)

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; __feature_set__:
Line &lt;SPAN class=""&gt;3297&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;if&lt;/SPAN&gt; self.sr &lt;SPAN class=""&gt;is&lt;/SPAN&gt; &lt;SPAN class=""&gt;None&lt;/SPAN&gt;:

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; sr:
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; self._data[self.name]

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\frame.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; __getitem__:
Line &lt;SPAN class=""&gt;3505&lt;/SPAN&gt;:  indexer = self.columns.get_loc(key)

File C:\Program Files\ArcGIS\Pro\&lt;SPAN class=""&gt;bin&lt;/SPAN&gt;\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\indexes\base.py, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; get_loc:
Line &lt;SPAN class=""&gt;3631&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;raise&lt;/SPAN&gt; KeyError(key) &lt;SPAN class=""&gt;from&lt;/SPAN&gt; err

KeyError: &lt;SPAN class=""&gt;None&lt;/SPAN&gt;
---------------------------------------------------------------------------&lt;/PRE&gt;&lt;P&gt;Also here is a screenshot of the CSV. I think something is happening with how pandas is creating the CSV. When trying to read the CSV file, it is not being read correctly. 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: 599px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/99638i6A03CEF76B8B668A/image-size/large?v=v2&amp;amp;px=999" role="button" title="JakeSkinner_0-1711622478796.png" alt="JakeSkinner_0-1711622478796.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I'm wondering if it has something to do with the delimiter being used or something else?&lt;/P&gt;&lt;P&gt;I'm very new to all of this so any help I could get would be greatly appreciated.&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 01 Apr 2024 15:53:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1403884#M70224</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-04-01T15:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: Overwriting hosted table in ArcGIS Online with CSV file from Pandas dataframe in Python</title>
      <link>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1403918#M70225</link>
      <description>&lt;P&gt;I also tried it this way and got a different error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;csvDF = pd.read_csv(outputCSVFile, sep=',')
adds = csvDF.to_dict("records")
crimeLayer.edit_features(adds=adds)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;---------------------------------------------------------------------------&lt;/SPAN&gt;
&lt;SPAN class=""&gt;Exception&lt;/SPAN&gt;                                 Traceback (most recent call last)
In  &lt;SPAN class=""&gt;[22]&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;1&lt;/SPAN&gt;:     crimeLayer.edit_features(adds=adds)

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\layer.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;edit_features&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;3408&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;return&lt;/SPAN&gt; &lt;SPAN class=""&gt;self&lt;/SPAN&gt;._con.post_multipart(path=edit_url, postdata=params)

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\_impl\_con\_connection.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;post_multipart&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;1270&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;return&lt;/SPAN&gt; &lt;SPAN class=""&gt;self&lt;/SPAN&gt;._handle_response(

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\_impl\_con\_connection.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;_handle_response&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;1008&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;self&lt;/SPAN&gt;._handle_json_error(data[&lt;SPAN class=""&gt;"&lt;/SPAN&gt;&lt;SPAN class=""&gt;error&lt;/SPAN&gt;&lt;SPAN class=""&gt;"&lt;/SPAN&gt;], errorcode)

File &lt;SPAN class=""&gt;C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\_impl\_con\_connection.py&lt;/SPAN&gt;, in &lt;SPAN class=""&gt;_handle_json_error&lt;/SPAN&gt;:
Line &lt;SPAN class=""&gt;1031&lt;/SPAN&gt;:  &lt;SPAN class=""&gt;raise&lt;/SPAN&gt; &lt;SPAN class=""&gt;Exception&lt;/SPAN&gt;(errormessage)

&lt;SPAN class=""&gt;Exception&lt;/SPAN&gt;: Cannot perform operation. Invalid operation parameters.
'adds' parameter is invalid
Object reference not set to an instance of an object.
(Error Code: 400)
&lt;SPAN class=""&gt;---------------------------------------------------------------------------&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 16:36:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1403918#M70225</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-04-01T16:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Overwriting hosted table in ArcGIS Online with CSV file from Pandas dataframe in Python</title>
      <link>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1403975#M70227</link>
      <description>&lt;P&gt;Hey, my initial guess from the key error is there's a missing column header. I noticed you are exporting to csv without taking out the index column. Can you try changing this line&lt;/P&gt;&lt;PRE&gt;trendstable.to_csv(outputCSVFile, sep=&lt;SPAN class=""&gt;'\t'&lt;/SPAN&gt;, encoding=&lt;SPAN class=""&gt;'utf-8'&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;P&gt;to&lt;/P&gt;&lt;PRE&gt;trendstable.to_csv(outputCSVFile, sep=&lt;SPAN class=""&gt;'\t'&lt;/SPAN&gt;, encoding=&lt;SPAN class=""&gt;'utf-8', index=False&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 18:15:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1403975#M70227</guid>
      <dc:creator>EarlMedina</dc:creator>
      <dc:date>2024-04-01T18:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Overwriting hosted table in ArcGIS Online with CSV file from Pandas dataframe in Python</title>
      <link>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1404055#M70229</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/86309"&gt;@EarlMedina&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Thanks so much for your reply! I noticed this earlier today and thought that might be an issue as well and so I tried it with index=False and with header=False. I also tried not defining the separator. When I open the csv in excel all the data is there. However, when I open it in ArcGIS Pro some of the fields show all the rows as Null. I wonder if this might have something to do with why it's not working. Same thing happens when I try loading it into the&amp;nbsp;GeoAccessor. I recently tried re-working my code to push the dataframe to an XLS instead of a CSV and then to a table. This seems to be working but I'm still not sure why the CSV is loading with null values. I'll post my code with the XLS work around if I can get it to fully work.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 20:45:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1404055#M70229</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-04-01T20:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Overwriting hosted table in ArcGIS Online with CSV file from Pandas dataframe in Python</title>
      <link>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1404097#M70234</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/86309"&gt;@EarlMedina&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I've got some code that works finally! By taking the dataframe and putting it into an XLS file and then pushing it to a table in a file geodatabase I was able to append to the hosted table from there:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# update hosted table from xls file
trendstable.to_excel(outputXLSFile)
trendstable = arcpy.ExcelToTable_conversion(outputXLSFile, outputTable, 'Sheet1')
trendstable = arcpy.management.Append(inputs=[outputTable], target=CrimeTrendsTableLyr.url)[0]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I'm still not sure why the CSV wouldn't work but I'm glad to have a solution!&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 23:47:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwriting-hosted-table-in-arcgis-online-with-csv/m-p/1404097#M70234</guid>
      <dc:creator>FredMitchell</dc:creator>
      <dc:date>2024-04-01T23:47:44Z</dc:date>
    </item>
  </channel>
</rss>

