1

Lets say I have three shapefiles/features I want to merge: 'florida.shp', 'mississipi.shp' and 'louisiana.shp'. They all have 3 standard fields common to all: 'DFIRMID', 'SFX', and 'FIRMPAN'. I want to do a merge of the three shapefiles on just those three fields. My output should be a 'gulfstates.shp'. I am a little confused as to how I would use {field_mappings} in merge. Since these are the only fields I want to keep in my merge and since they are common to all three shapefiles, could I literally just use field mapping to remove all other fields? Is there an easier way to do this?

fieldMappings = arcpy.FieldMappings()
fieldMappings.addTable('florida.shp')
fieldMappings.addTable('mississipi.shp')
fieldMappings.addTable('louisiana.shp')
for field in fieldMappings.fields:
    if field.name not in ['DFIRMID', 'SFX','FIRMPAN']:
        fieldMappings.removeFieldMap(fieldMappings.findFieldMapIndex(field.name))
3

One way to do this is with the Append tool (and depending on the amount of common fields among your shapefiles, you may have to modify the field mappings). Pick one of your three shapefiles (or make a copy of it), and append the other two shapefiles to it. Within the tool, select No Test and ensure the three common fields match (right click on field name in Field Map section to modify matching field). Below is the original FC One attribute table. I made a copy of this feature class to use to append (you would rename this copy to gulfstates.shp).

enter image description here

Then I run the append with the copied feature class as the target and I append only the FC Two feature class. The Field Mappings match the common fields automatically.

enter image description here

If you wanted to do this with code, you could run the tool using the GUI as I have done, then click on the Results window, right click the Append process, and Save as Python Snippet. Then paste and modify this code as necessary.

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.