'python to replace records in a feature layer field based on a record from a field in the csv file
I tried to write a python script to replace the records in the field called "fieldID" of the feature layer (fl) in the ArcGIS online based on a record from a field called "FLAT" in the csv file. The two common IDs in the feature layer and the csv file are Field_ID and CSV_ID, respectively.
Below is what I have and what I expect to see in the feature layer. [what I have and what I expect to see][1] [1]: https://i.stack.imgur.com/l3RpE.png
I have tried this code, thanks to HuubZwart, but it did not work.
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
fields = [field["name"] for field in fs["fields"] if field["name"] in df.columns]
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)*
I got this message "Parameters not valid for edit_features"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
