I have the following code that was provided by bixb0012 , thank you for the merge code. I need to be able to keep a certain fields attributes/ or transfer them once the merge is done. With the following code some merged features have info in the Field2 but some doesn't. I would like to merge the features that have the same attribute from Field1 but keep/add what is in Field 2 to the new merged Field2.
Original features Merged Features
ObjectID Field1 Field 2 --> ObjectID Field1 Field2
01 CP0001 Conditional --> 1 CP0001 Conditional
02 CP0001 'Blank'
03 CP0001 'Blank'
04 CP0001 'Blank'
01 CP0014 Relocation --> 2 CP0014 Relocation
02 CP0014 'Blank'
03 CP0014 'Blank'
04 CP0014 'Blank'
from arcpy import da
from itertools import groupby
from operator import itemgetter
from functools import reduce
fc = # path to feature class or table
case_fields = ["field1", "field2"] # field(s) to group records for merging
sort_field, sort_order = "OBJECTID", "ASC"
shape_field = "SHAPE@"
fields = case_fields + [sort_field, shape_field]
sql_orderby = "ORDER BY {}, {} {}".format(", ".join(case_fields), sort_field, sort_order)
kwargs = {"in_table":fc, "field_names":fields, "sql_clause":(None, sql_orderby)}
with da.UpdateCursor(**kwargs) as ucur, da.SearchCursor(**kwargs) as scur:
case_func = itemgetter(*range(len(case_fields)))
ugroupby, sgroupby = groupby(ucur, case_func), groupby(scur, case_func)
for (ukey,ugroup),(skey,sgroup) in zip(ugroupby,sgroupby):
poly = reduce(arcpy.Geometry.union, (row[-1] for row in sgroup))
row = next(ugroup)
ucur.updateRow(row[:-1] + [poly])
for row in ugroup:
ucur.deleteRow()<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>