Arcpy – How to Join Pandas DataFrame to Shapefile

arcpyattribute-joinspandas

I have a shapefile:

Street1    Street2    Street3
Field      Clark      Taylor
Field      Jackson    State
Franklin   23rd       25th

And a Dataframe df:

Page    Entry_Date
12      08012018
17      07252018
11      07252018

The only commonalities between the two are the index position of the rows. I'd like to join them together so my result is as below in my shapefile

Street1    Street2    Street3    Page    Entry_Date
Field      Clark      Taylor     12      08012018
Field      Jackson    State      17      07252018
Franklin   23rd       25th       11      07252018

I've tried a couple ways but it doesn't seem that arcpy likes to cooperate with pandas

in_shp = "C:\\Users\\Anthony\\Proj56\\ExportShp.shp"

for idx, row in df.itterrows():
    arcpy.JoinField_management(in_shp, "FID", df, idx, [df.columns.values])

This just gives me an error. Is there any way to incorporate a pandas dataframe into an existing shapefile without completely rewriting everything?

Best Answer

For those curious, I converted the dataframe into a numpy array and then stored the table in_memory, joined my records, then exported out back to a shapefile. Not the cleanest workaround but a workaround all the same.

x = np.array(np.rec.fromrecords(df.values))
names = df.dtypes.index.tolist()
x.dtype.names = tuple(names)
arcpy.da.NumPyArrayToTable(x, "in_memory/mytable")
...
arcpy.FeatureClassToFeatureClass_conversion("in_memory/mytable", OutFolder, "NewShp")