1

In ArcMap 10.6, I'm merging a series of layers with shared attribute fields. After I merge, every field is duplicated instead of combined, so it looks like this (real thing too wide for screenshot):

    FieldA   FieldB   FieldA     FieldB
      y        a      <NULL>     <NULL>
      x        b      <NULL>     <NULL>
    <NULL>   <NULL>      x          b
    <NULL>   <NULL>      x          b

What is the code needed in Field Calculator to instruct "FieldA = if Null, = value of other Field A, if not Null, leave as is"

Desired result:

    FieldA   FieldB   
      y        a      
      x        b      
      x        b
      x        b

The answer here Field calculator if and elseif statement with <Null> values in ArcGis 10.1 was close but not quite. I think I might be doing something fundamentally wrong in my merge for the attribute table to form this way. The layers being merged have been joined to the same .csv prior to the merge.

2

With the Field Mapping option of Merge you should be able to prevent duplicate fields by telling the tool to merge certain fields together and to keep the values which are not None.

If this is not an option you can use arcpy. I am assuming you have duplicate field aliases since it should not be possible to have duplicate field names. You can execute the code in the python window of ArcMap after adjusting some of the commented lines:

import arcpy
from itertools import groupby

arcpy.env.workspace = r'C:\folder\database.gdb' #Change to match your database
original_fc = 'Duplicates' #Change name to match your feature class
copy_fc = 'Dup2' #Change name. The copy is being used in the calculations so the original fc is not corrupted

fieldidentifier = 'Field' #Fields with aliases starting with this will be combined

arcpy.CopyFeatures_management(in_features=original_fc, out_feature_class=copy_fc)

fields = [f for f in arcpy.ListFields(dataset=copy_fc) if f.aliasName.startswith(fieldidentifier)] #List fields starting with fieldidentifier
fields_grouped = [list(v) for k,v in groupby(fields, lambda x: x.aliasName)] #Group them together by common field alias

#Update first field in each group with non-null values
for group in fields_grouped:
    if len(group)>1:
        with arcpy.da.UpdateCursor(copy_fc, [f.name for f in group]) as cursor:
            for row in cursor:
                row[0] = [i for i in row if i is not None][0]
                cursor.updateRow(row)
        #Delete all but first field in each group
        for field in group[1:]:
            arcpy.DeleteField_management(in_table=copy_fc, drop_field=field.name)

enter image description here

1

To avoid this issue:

1) After a join, export the layer so the new shapefile keeps the data permanently

2) In Layer Properties, under the field tab, ensure that all field Names are the same among layers to be merged. Use the options drop down to make sure you're inspecting Names and not Aliases, which for me were masking discrepancies among field names

As a final check, in the Field Map box of the Merge tool, field names should be common among all layers. If not, prior to merging fix the names. To change a field name, create a new field and copy the data over from the "misnamed" field (then delete the old field).

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.