4

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?

1
  • 1
    Correct me if I'm wrong, but I don't think arcpy has any kind of pandas integration. It does have some functions to work with numpy arrays, which is what pandas uses on the backend. Pull out a numpy array, use arcpy.NumPyArrayToTable(), and join that
    – mikewatt
    Aug 15, 2018 at 17:51

2 Answers 2

4

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")
0

Another approach is to export the data frame to a scratch csv file using df.to_csv(filepath) then use the arcpy.JoinField_management tool to join the scratch csv file to the shapefile, and finally delete the scratch file (or not).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.