I need to aggregate data with the same id's into one row, but save the different Code's into different columns.
I look into this post: How to pivot a dataframe, but this post does not answer my question.
For example: the input dataframe can be:
d = {'id': ['a', 'a', 'a', 'b', 'b', 'b'], 'Code': ['A1', 'A2', 'A3', 'B1', 'B2', 'B3']}
df1 = pd.DataFrame(data=d)
print(df1)
   id  Code
0   a    A1
1   a    A2
2   a    A3
3   b    B1
4   b    B2
5   b    B3
But the output needs to be like this:
d = {'id': ['a', 'b'], 'Code_1': ['A1', 'B1'], 'Code_2':['A2', 'B2'], 'Code_3':['A3', 'B3']}
df2 = pd.DataFrame(data=d)
print(df2)
   id  Code_1  Code_2  Code_3
0   a    A1      A2      A3
1   b    B1      B2      B3
I tried multiple ways. Closest was code i found on a different question that did at least the aggregation part
df1.groupby('id')['Code'].apply(' '.join).reset_index()
But that gives an output where the different codes are saved into one column as one long string, not saved into multiple columns.
  id      Code
0  a  A1 A2 A3
1  b  B1 B2 B3
Any help would be great!
 
    