I have a table that has a group name column and another column that has ID values. Instead of having a long table I want it to be wide. My ID values are strings. Below is an example of my table
df = {'group_name': ["ACE", "ACE", "ACE", "ACE"],
'data_source_palma_id': ["ACE(SIS)", "ACE(MAG)"," ACE(EPAM)", "ACE(SWEPAM)"]
}
group_name data_source_palma_id
0 ACE ACE(SIS)
1 ACE ACE(MAG)
2 ACE ACE(EPAM)
3 ACE ACE(SWEPAM)
I want the table to look like this. It can either have new column names for each id( I think that's too hard) I'm fine with the ID column being a list of each ID that is associated with the group name.
group_name data_source_palma_id
0 ACE [ACE(SIS), ACE(MAG), ACE(EPAM), ACE(SWEPAM)]
This somewhat works but I get the IDx column at a weird position. Is there a better way to do this?
df['idx'] = df.groupby('group_name').cumcount()
df.pivot(index='group_name',columns='idx')[['data_source_palma_id']]
data_source_palma_id
idx 0 1 2 3
group_name
ACE ACE(SIS) ACE(MAG) ACE(EPAM) ACE(SWEPAM)