I want to join the attribute table of a shapefile with an externally created csv table via ArcPy.

The format of the columns are:

for attribute table: "Variable1" "Variable2" "Variable3" "CATCHID" "Variable 4"...

for csv table: "CATCHID" "Results1" "Results2"...

I want to append the attribute table with the contents of the csv table with respect to the "CATCHID" column. Since my csv table doesn't have OID's, I couldn't use JoinField_management function. Hopefully, there's another function named AddJoin_management, it is really useful when used in ModelBuilder, but it is a mess in ArcPy since it requires a table view/feature layer as an input. I made a feature layer, joined two tables and copied the feature layer to a brand new shapefile but this time the names of the columns were changed as "[attributetablename].Variable1", "[attributetablename].Variable2" etc. and they are truncated while creating the new shapefile.

Is there a way to join two tables PROPERLY using ArcPy? (which means I don't want to see the layer names predicate the variable names in the columns.)

inputShapefile = r"C:\\input.shp"
inputPath = r"C:\\input.csv"
outputPath = r"C:\\output.shp"

arcpy.MakeFeatureLayer_management(inputShapefile, "tempLayer")
arcpy.AddJoin_management("tempLayer", "CATCHID", inputPath, "CATCHID", "KEEP_COMMON")
arcpy.CopyFeatures_management("tempLayer", outputPath)

Here's the code snippet that I have problem with. This join operation produces a new shapefile that includes "[input].CATCHID" as a column header, for instance. However I need just "CATCHID" as the header.

  • 1
    Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. A question asking for help writing code should include a snippet of your code. What have you tried? Please edit your question to include a snippet of the code you've tried and details of the results when it is run. – Midavalo Oct 20 '17 at 14:53
  • 3
    Copy your CSV to an in_memory temporary table (which will create the OID) then use JoinField_management() to join to your table – Midavalo Oct 20 '17 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.