I am testing out some code in the Python window of ArcGIS so that I can insert it into a standalone script later. I want to create a copy of a table and add a new field that is the result of concatenating two of the existing fields using a space as a delimiter.

I am sort of following the suggestion below to use field mapping which is from this page http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000065000000 because I ultimately want to use the JoinField command to join two tables based on two attributes: "To merge two or more fields in the join table before the join, first export the table or feature class using the Table To Table tool and merge using the tool's field map."

My code does the concatenation or merging just fine, but I cannot get it to rename the output field. In my resulting table, the name of the new field is the same as the first field I add to the fieldmap (ie, "COC_NAME" in the code below.)

fieldMappings = arcpy.FieldMappings()
fieldmap_join = arcpy.FieldMap()
fieldmap_join.joinDelimiter = " "
fieldmap_join.mergeRule = "Join"
fieldmap_join.addInputField("COC_DATA", "COC_NAME")
fieldmap_join.addInputField("COC_DATA", "GRID_ID")
fieldjoin = fieldmap_join.outputField
fieldjoin.name = "JOINID"
fieldmap_join.outputField = fieldjoin
arcpy.TableToTable_conversion("COC_DATA", "C:\\HEA_GIS.mdb", "COC_MOD", "", fieldMappings)

Can someone point out what am I doing incorrectly or perhaps provide another solution?

  • Hmm at first I thought you hadn't assigned the field back to the field mappings object, but you do have that. Is there any change if you modify the output field (and assign back) at the end, after adding the tables and such to it? I'm also not sure why you would need to do this in the first place unless you don't want to modify the input table (just add a field and calculate it). – Evil Genius Jul 18 '14 at 16:49
  • In my script, I originally did create a new field and calculate it, but the table has 191,000 rows and it was taking a really long time to calculate. So, I was trying to find a faster solution. – jencarta Jul 18 '14 at 17:11

You are attempting to add 2 input fields to a single field map object. Field map object can only assign one output field to a one input field. You will need to create two seperate field map objects one for COC_NAME and one for GRID_ID. For a cleaner method, I suggest creating a cross walk dictionary {input:output}, iterate through dict pairs, and place your field map object inside the loop.

Also, the addTable method for the FieldMappings object loads the target dataset and should be called immediately after you the create the FieldMappings object.

| improve this answer | |
  • Thank you for replying. I did end up creating a join dictionary based on a solution I found online. I forgot that I had posted this question, so I will put my answer up. – jencarta Apr 21 '16 at 12:21

I scrapped the idea of using field mappings to join my table. Instead, I created a join dictionary to join my tables with. I found a similar solution on Stack Exchange which I modified slightly to use in my script. Here is the original post: Most efficient method to join multiple fields in ArcGIS. My solution:

        joinfields = ['GRID_ID', 'FOOTPRINT_ID']
        joindict = {}
        with arcpy.da.SearchCursor(COC_FP, joinfields) as rows:
            for arow in rows:
                joinval = arow[0]
                val1 = arow[1]
        del arow, rows
        targetflds = ['GRID_ID', 'FOOTPRINT_ID']
        expression2 = arcpy.AddFieldDelimiters(COCTbl, "COC_NAME") + " = '" + COCName + "'"
        with arcpy.da.UpdateCursor(COCTbl, targetflds, where_clause=expression2) as recs:
            for rec in recs:
                keyval = rec[0]
                rec[1] = joindict[keyval]
        del rec, recs
| improve this answer | |

Your Answer

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

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