I have a dataset with 600 rows. the data has one main ID= Version and second ID= Task. Data looks like this:
Version Task Concept Att 1 - Att 2 -
1 1 1 3 2
1 1 2 1 1
1 2 1 2 3
1 2 2 1 2
1 3 1 2 3
1 3 2 3 1
2 1 1 2 1
2 1 2 3 2
2 2 1 2 2
2 2 2 1 3
2 3 1 3 1
2 3 2 1 3
I would like to change the format, so to have "Task" which belongs to the same "Version" in the same row like this:
Version Task Concept Att 1 - Att 2 - Version Task Concept Att 1 - Att 2 -
1 1 1 3 2 1 1 2 1 1
1 2 1 2 3 1 2 2 1 2
1 3 1 2 3 1 3 2 3 1
2 1 1 2 1 2 1 2 3 2
2 2 1 2 2 2 2 2 1 3
2 3 1 3 1 2 3 2 1 3
I have tried different things like groupby, pivot but I cannot find the right solution
>Solution :
I think a pivot is the clean way to reshape (df.pivot(index=['Version', 'Task'], columns='Concept'), optionally with flattening the columns MultiIndex).
That said if you really want to duplicate the columns, you could combine a groupby and concat:
out = (pd.concat([g.set_index(['Version', 'Task'], drop=False)
for k, g in df.groupby('Concept')], axis=1)
.reset_index(drop=True)
)
Output:
Version Task Concept Att 1 - Att 2 - Version Task Concept Att 1 - Att 2 -
0 1 1 1 3 2 1 1 2 1 1
1 1 2 1 2 3 1 2 2 1 2
2 1 3 1 2 3 1 3 2 3 1
3 2 1 1 2 1 2 1 2 3 2
4 2 2 1 2 2 2 2 2 1 3
5 2 3 1 3 1 2 3 2 1 3