Suppose we have a Dataframe looks like this
df = pd.DataFrame(columns=['A', 'B','C'])
df.loc[0]=[1,2,3]
df.loc[1]=[4,5,6]
df.loc[2]=[7,8,9]
df.loc[3]=[10,11,12]
df.loc[4]=[13,14,15]
df.loc[5]=[16,17,18]
df.loc[6]=[19,20,21]
df
A B C
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
4 13 14 15
5 16 17 18
6 19 20 21
I want to modify df to get df2;
df2 = pd.DataFrame(columns=['first', 'second','third','fourth','fifth','sixth'])
df2.loc[0]=[1,2,4,5,7,8]
df2.loc[1]=[4,5,7,8,10,11]
df2.loc[2]=[7,8,10,11,13,14]
df2.loc[3]=[10,11,13,14,16,17]
df2.loc[4]=[13,14,16,17,19,20]
df2
first second third fourth fifth sixth
0 1 2 4 5 7 8
1 4 5 7 8 10 11
2 7 8 10 11 13 14
3 10 11 13 14 16 17
4 13 14 16 17 19 20
That is, I want to fill the first row of df2 by the three rows of the first two columns of df. Then we proceed to fill the second row of df2 by the next three rows of the two columns of df, and etc.
What should I do to move from df to df2? I can do some elementary and simple manipulation. But it looks still hard for me now.
Can anyone help me, please?