Select to view content in your preferred language

Updating Fields in a Feature Layer Using a Dataframe

2295
3
03-04-2021 02:10 PM
MackWood
Occasional Contributor

I am trying to update fields in a feature layer using a dataframe that I read in via a csv file in order to update a map on an annual basis. I am not sure how to use the edit_features command to update the fields based on a join of a unique key between the dataframe and the feature layer.

This is what I have so far. I am trying to update the fields rpl_theme1 through rpl_themes but retain the geometry. So I am hoping there is a way to use some code to look for the unique key BOROUGH_TRACT in the feature layer and update the fields using the dataframe.

The feature layer has the following data:

MackWood_0-1614895641040.png

And the dataframe the following:

MackWood_1-1614895667303.png

 

from arcgis.gis import GIS
gis = GIS("home")

#set file paths
overall_rank = '/arcgis/home/overall_rank_2019.csv'
#overall_flags = '/arcgis/home/overall_flags_2019.csv'
#theme1_rank = '/arcgis/home/theme1_rank_2019.csv'
#theme1_flags = '/arcgis/home/theme1_flags_2019.csv'
#theme2_rank = '/arcgis/home/theme2_rank_2019.csv'
#theme2_flags = '/arcgis/home/theme2_flags_2019.csv'
#theme3_rank = '/arcgis/home/theme3_rank_2019.csv'
#theme3_flags = '/arcgis/home/theme3_flags_2019.csv'
#theme4_rank = '/arcgis/home/theme4_rank_2019.csv'
#theme4_flags = '/arcgis/home/theme4_flags_2019.csv'

#import packages
import pandas
import arcgis
import numpy as np
import arcpy

#read csv files with borough and tract as strings to retain leading 0s
overall_rank_df = pandas.read_csv(overall_rank, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#overall_flags_df =  pandas.read_csv(overall_flags, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#theme1_rank_df = pandas.read_csv(theme1_rank, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#theme1_flags_df = pandas.read_csv(theme1_flags, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#theme2_rank_df = pandas.read_csv(theme2_rank, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#theme2_flags_df = pandas.read_csv(theme2_flags, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#theme3_rank_df = pandas.read_csv(theme3_rank, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#theme3_flags_df = pandas.read_csv(theme3_flags, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#theme4_rank_df = pandas.read_csv(theme4_rank, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})
#theme4_flags_df = pandas.read_csv(theme4_flags, converters={'BOROUGH': lambda x: str(x), 'tract': lambda x: str(x)})

overall_rank_df["BOROUGH_TRACT"]=overall_rank_df["BOROUGH"]+overall_rank_df["tract"]

HEI_Features_itemID = "fa044b611f7d4f77a57d4c0e42edbe2e"
HEI_Features_Item = gis.content.get(HEI_Features_itemID)

overall_rank_flayer = HEI_Features_Item.layers[4]
overall_rank_fset = overall_rank_flayer.query() #querying without any conditions returns all the features
overall_rank_flayer_rows = overall_rank_fset.sdf

overall_rank_flayer_rows

overall_rank_flayer.properties.capabilities

overall_rank_df

 

 

0 Kudos
3 Replies
by Anonymous User
Not applicable

Hi MackWood my understanding is you want to update a feature layer's attribute table according to a dataframe. If this is the case, i think we can use the arcpy.da.UpdateCursor to archive this. It would be something like this: 

#import excel to dataFrame
df = pandas.read_excel(fileName,sheetName,header = None,parse_cols = [0])
df.columns=['rpl_theme']

fc = r"C:/your/path"
field = ['rpl_theme1']

with arcpy.da.UpdateCursor(fc, field) as cursor:
for row in cursor:
if row[0] == rpl_theme:
row[0] = rpl_theme1
cursor.updateRow(row)

JimmyWang
Occasional Contributor

I got the same thing.

0 Kudos
MehdiPira1
Esri Contributor

Hi @MackWood ,

You can follow the link below from esri sample notebooks (Apply updates from the second spreadsheet). This provides a step by step guide from reading a csv file, a feature layer, joining them and finally updating the attribute fields based on your csv dataframe. You just need to skip the geometry updates.

https://developers.arcgis.com/python/sample-notebooks/updating-features-in-a-feature-layer/

 

I hope that helps.

Mehdi