I need to transform the following data from long to wide format. The issue is the the group is name is potentially duplicating. Also, the number of duplicating entries is unknown so I need to account for that as well.
This is the desired result:
desired_data = [[1,'Joe Tell','111-222-3333','Al Mead','222-333-4444','Ted Shaw','444-344-2323'],
[2,'Don Roads','555-222-5213','','','Ted Frank','222-444-2323']]
df_result = pd.DataFrame(desired_data, columns = ['ID', 'Primary 1 - Name','Primary 1 - Phone',
'Primary 2 - Name','Primary 2 - Phone',
'Secondary 1 - Name','Secondary 1 - Phone'])
I have tried pivoting the data but it errors on the duplicate group issue. I have tried manually adding a prefix to the "Sequence" name but it is getting messy.
data = [[1,'Joe Tell','Primary','111-222-3333'],
[1,'Al Mead','Primary','222-333-4444'],
[1,'Ted Shaw','Secondary','444-344-2323'],
[2,'Don Roads','Primary','555-222-5213'],
[2,'Ted Frank','Secondary','222-444-2323']]
df = pd.DataFrame(data, columns = ['ID', 'Name','Sequence','Phone'])
df.pivot(index='ID', columns='Sequence', values=['Name','Phone'])