Kapital :
I have the following pandas dataframe.
ID Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9
1 A B C A B C A B C
2 D E F D E F D E F
I would like to get the following table by restructuring every three-column values.
ID Col_1 Col_2 Col_3
1 A B C
A B C
A B C
2 D E F
D E F
D E F
OR
ID Col_1 Col_2 Col_3
1 A B C
1 A B C
1 A B C
2 D E F
2 D E F
2 D E F
Is there any efficient way to do it? I tried to find similar examples here at the StackOverflow community but couldn't. If you have, you can point me.
Any help is appreciated!
anky_91 :
Here is one with apply
and np.reshape
:
f = lambda x: pd.DataFrame(np.reshape(x.to_numpy(),(-1,3))).add_prefix('Col_')
df.groupby('ID').apply(f).reset_index('ID')
ID Col_0 Col_1 Col_2
0 1 A B C
1 1 A B C
2 1 A B C
3 2 D E F
4 2 D E F
5 2 D E F