<?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: python to update a field in a feature layer based on records in csv file in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149145#M7186</link>
    <description>&lt;P&gt;Can you share what the structure of "edits" is now?&lt;/P&gt;</description>
    <pubDate>Tue, 01 Mar 2022 16:10:52 GMT</pubDate>
    <dc:creator>HuubZwart</dc:creator>
    <dc:date>2022-03-01T16:10:52Z</dc:date>
    <item>
      <title>python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1148731#M7172</link>
      <description>&lt;P&gt;Hello:&lt;/P&gt;&lt;P&gt;I am a little bit new to coding. I would like to update an existing feature layer in AGOL using an open-source python library. Only one field (let us say field1) needs to be updated in the feature layer based on source data in csv. The feature layer and the .csv file have a common field to join them. Field_ID in the feature layer and CSV_ID in CSV file are used to join or map the source and the target. I do not think it matters but Field_ID is not unique. I am looking for a python code to automate the process of updating features in the feature layer using data from csv. I can not overwrite the feature layer because there are records or fields in the feature layer that are not in the csv.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is what I have...&lt;/P&gt;&lt;P&gt;import pandas as pd&lt;/P&gt;&lt;P&gt;import os&lt;/P&gt;&lt;P&gt;from arcgis.gis import GIS&lt;/P&gt;&lt;P&gt;from arcgis import features&lt;/P&gt;&lt;P&gt;from arcgis.features import SpatialDataFrame&lt;/P&gt;&lt;P&gt;from arcgis.mapping import WebMap&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;from arcgis.features import FeatureLayerCollection&lt;/P&gt;&lt;P&gt;csvFile = pd.read_csv("csvfile.csv")&lt;/P&gt;&lt;P&gt;portal ="htttps://xyz.maps.arcgis.com/"&lt;/P&gt;&lt;P&gt;username&amp;nbsp; = "abc"&lt;/P&gt;&lt;P&gt;pw = "pword"&lt;/P&gt;&lt;P&gt;gis = GIS(portal, username, pw)&lt;/P&gt;&lt;P&gt;Feature_layerID&amp;nbsp; = gis.content.get("123456789")&lt;/P&gt;&lt;P&gt;#replace the records in the "field1" of the feature layer based on the data in csv file, using the two common IDs in the feature layer and the csv file (Field_ID in the feature layer and CSV_ID in CSV file are used to join or map the source and the target).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 19:07:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1148731#M7172</guid>
      <dc:creator>Nahomdinka</dc:creator>
      <dc:date>2022-02-28T19:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1148860#M7173</link>
      <description>&lt;P&gt;Not tested and this will overwrite the data in your feature layer. More could be done like mapping different names, only apply those edits that are actually changed etc. But this should get you on the right track.&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;# Feature_layerID is a bit confusing, gis.get("some id") returns a feature layer collection, so I changed that
fl_id = "abcde"
flc = gis.content.get(fl_id)
layer_id = 0 # index of the layer in the flc
fs = flc.layers[layer_id].query(where="1=1", returnGeometry=False).to_dict()
fields = [field["name"] for field in fs["fields"]] # gets the fields from your fl 
features = fs["features"] # the existing features
csv_file_path = # your absolute file path
df = pd.read_csv(csv_file_path) #read the csv,  add sep= to specify separator if neccessary
update_dict = df.set_index("CSV_ID")[fields].to_dict("index") # get a dictionary with the ID field as index 
edits = [] # empty list to fill with features to update
for f in features:
    updated_attributes = update_dict.get(f["attributes"]["Field_ID"]) # get the new values from the dictionary
    if(updated_attributes is not None):
        f["attributes"].update(updated_attributes) # update the feature, all values from the csv will overwrite the old ones 
        edits.append(f)

# apply the edits to the feature layer
flc.layers[layer_id].edit_features(updates=edits)&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>Tue, 01 Mar 2022 16:09:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1148860#M7173</guid>
      <dc:creator>HuubZwart</dc:creator>
      <dc:date>2022-03-01T16:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149108#M7176</link>
      <description>&lt;P&gt;Thanks a lot,&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/112531"&gt;@HuubZwart&lt;/a&gt;&amp;nbsp;. I am almost there. I believe that on line #8, you meant to say [layer_id].&lt;/P&gt;&lt;PRE&gt;fs = flc.layers[layer_id).query(where="1=1", returnGeometry=False).to_dict()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then, I got an error after #10.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;update_dict = df.set_index("CSV_ID")[fields].to_dict("index")&lt;/PRE&gt;&lt;P&gt;The error says...&lt;/P&gt;&lt;P&gt;KeyError: "None of [Index([;ogc_fld;, 'name', 'field3', 'field4'))] are in the [columns]"&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 15:20:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149108#M7176</guid>
      <dc:creator>Nahomdinka</dc:creator>
      <dc:date>2022-03-01T15:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149125#M7177</link>
      <description>&lt;P&gt;You're right - I edited my original reply.&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;KeyError: "None of [Index([;ogc_fld;, 'name', 'field3', 'field4'))] are in the [columns]"&lt;/P&gt;&lt;P&gt;This means these fields are in your feature layer, but not in the csv. To circumvent this we can select only the the fields from the feature layer that are also in the csv. Make sure to move this below the declaration of df.&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fields = [field["name"] for field in fs["fields"] if field["name"] in df.columns]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 15:48:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149125#M7177</guid>
      <dc:creator>HuubZwart</dc:creator>
      <dc:date>2022-03-01T15:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149135#M7182</link>
      <description>&lt;P&gt;Thanks again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got an error on the last step...&lt;/P&gt;&lt;PRE&gt;# apply the edits to the feature layer
flc.layers[layer_id].applyEdits(updates=edits)&lt;/PRE&gt;&lt;P&gt;AttributeError: 'FeatureLayer' object has no attribute 'applyEdits'&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 16:01:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149135#M7182</guid>
      <dc:creator>Nahomdinka</dc:creator>
      <dc:date>2022-03-01T16:01:53Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149138#M7183</link>
      <description>&lt;P&gt;My bad had the REST function in mind, that should be&lt;/P&gt;&lt;PRE&gt;flc.layers[layer_id].edit_features(updates=edits)&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 16:03:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149138#M7183</guid>
      <dc:creator>HuubZwart</dc:creator>
      <dc:date>2022-03-01T16:03:44Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149140#M7184</link>
      <description>&lt;P&gt;NP.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once I run&lt;/P&gt;&lt;PRE&gt;flc.layers[layer_id].edit_features(updates=edits)&lt;/PRE&gt;&lt;P&gt;, I got this message "Parameters not valid for edit_features"&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 16:06:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149140#M7184</guid>
      <dc:creator>Nahomdinka</dc:creator>
      <dc:date>2022-03-01T16:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149145#M7186</link>
      <description>&lt;P&gt;Can you share what the structure of "edits" is now?&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 16:10:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149145#M7186</guid>
      <dc:creator>HuubZwart</dc:creator>
      <dc:date>2022-03-01T16:10:52Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149152#M7188</link>
      <description>&lt;P&gt;The aim is to replace&amp;nbsp;&lt;SPAN&gt;the records in the field called "field1" of the feature layer (fl) based on a record from a field called "FLAT"&amp;nbsp; in the csv file. The two common IDs in the feature layer and the csv file are Field_ID and CSV_ID, respectively.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 16:21:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149152#M7188</guid>
      <dc:creator>Nahomdinka</dc:creator>
      <dc:date>2022-03-01T16:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149183#M7190</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/112531"&gt;@HuubZwart&lt;/a&gt;&amp;nbsp; Here is what it looks like&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nahomdinka_0-1646154820382.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/35257i54EDACD0561406D8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Nahomdinka_0-1646154820382.png" alt="Nahomdinka_0-1646154820382.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 17:14:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149183#M7190</guid>
      <dc:creator>Nahomdinka</dc:creator>
      <dc:date>2022-03-01T17:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: python to update a field in a feature layer based on records in csv file</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149341#M7192</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/920"&gt;@JeffBigos&lt;/a&gt;&amp;nbsp;- can you assist the Python question here?&amp;nbsp; The customer - Nohomdinka - requested my assistance but I do not program in Python so am out of my wheelhouse here.&amp;nbsp; Thx!&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2022 22:03:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/python-to-update-a-field-in-a-feature-layer-based/m-p/1149341#M7192</guid>
      <dc:creator>Robert_LeClair</dc:creator>
      <dc:date>2022-03-01T22:03:11Z</dc:date>
    </item>
  </channel>
</rss>

