0

I need to aggregate data with the same id's into one row, but save the different Code's into different columns. For example: the input dataframe can be:

d = {'id': ['a', 'a', 'a', 'b', 'b', 'b'], 'Code': ['11', '12', '13', '14', '15', '16']}
df10 = pd.DataFrame(data=d)
print(df10)

   id  Code
0   a    11
1   a    12
2   a    13
3   b    14
4   b    15
5   b    16

But the output needs to be like this:

d = {'id': ['a', 'b'], 'Code': ['11', '14'], 'Code_2':['12', '15'], 'Code_3':['13', '16']}
df11 = pd.DataFrame(data=d)
print(df11)

   id  Code  Code_2  Code_3
0   1    11      12      13
1   2    14      15      16

I tried multiple ways. Closest was code i found on a different question that did at least the aggregation part

df10.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  11 12 13
1  b  14 15 16

Does anyone know how to do this? Big thanks :)

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
Laura
  • 45
  • 7

0 Answers0