0

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)
cjaka301
  • 41
  • 3
  • already asked and answered here: https://stackoverflow.com/questions/22798934/pandas-long-to-wide-reshape-by-two-variables – keithpjolley Dec 21 '20 at 22:03
  • 3
    Does this answer your question? [Pandas long to wide reshape, by two variables](https://stackoverflow.com/questions/22798934/pandas-long-to-wide-reshape-by-two-variables) – keithpjolley Dec 21 '20 at 22:05
  • It sort of works because I get the id values in 1 row but, I get a messy dataframe. – cjaka301 Dec 22 '20 at 01:54

0 Answers0