1

I have a table that looks like the following

ID    Address                      Date
1     11 abc road                  19/03/2018
2     11 def road                  20/03/2018
2     11 def road                  21/03/2018
3     11 qaz road                  25/03/2018
4     11 zxc road                  26/03/2018
4     11 zxc road                  27/03/2018
6     11 qwe road                  18/03/2018
6     11 qwe road                  19/03/2018
6     11 qwe road                  20/03/2018

What I would like to do, is to merge all the rows that have the same id, into one record but merge the differing dates into that same record aswell.

So the final table would look like

ID    Address                Date
1     11 abc road            19/03/2018
2     11 def road            20/03/2018, 21/03/2018
3     11 qaz road            19/03/2018
4     11 zxc road            26/03/2018, 27/03/2018
6     11 qwe road            18/03/2018, 19/03/2018, 20/03/2018

What is the best pythonic way of achieving this?

2

You didn't specify whether this was just a table or has geometry, but this strategy would work for both:
1. Add a new field to hold the concatenated dates
2. Populate this field:

# Make a dictionary to hold the "merged' dates
addressDict = {row[0]: [] for row in arcpy.da.SearchCursor('mylayer', ['Date'])}

# Populate the dictionary with the merged dates
with arcpy.da.SearchCursor('mylayer', ['Address', 'Date']) as cursor:
    for row in cursor:
        addressDict[row[0]].append(row[1])

# Add this to a new merged date field (adding field not shown here)
with arcpy.da.UpdateCursor('mylayer', ['Address', 'Date', 'MyMergedDates']) as ucursor:
    for row in ucursor:
        row[2] = ', '.join([str(n) for n in addressDict[row[0]]])
        ucursor.updateRow(row)

3. Use the normal merge tool

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .