Home > Mobile >  Reconstruct columns by alternatively putting the values in column -> pandas
Reconstruct columns by alternatively putting the values in column -> pandas

Time:05-12

I have the following dataframe:

df1=[[1,1,2,2],[3,3,4,4]]

df1 = pd.DataFrame(df1)

   0  1  2  3
0  1  1  2  2
1  3  3  4  4

I want to reconstruct this dataframe so it looks like the following:

df=[[1,1],[2,2],[3,3],[4,4]]

df = pd.DataFrame(df)

   0  1
0  1  1
1  2  2
2  3  3
3  4  4

There must be a neat pythonic way to do this. Any help would be appreciated.

CodePudding user response:

You can reshape it

out = pd.DataFrame(df1.to_numpy().reshape((-1,2)))
Out[545]: 
   0  1
0  1  1
1  2  2
2  3  3
3  4  4
  • Related