Home > Enterprise >  Reshaping DataFrame columns
Reshaping DataFrame columns

Time:06-18

I need to reshape a DataFrame with the following shape:

sample_df
Out[97]: 
                    0       1                   2       3
0  2011-01-01 00:00:00  123.39  2012-01-01 00:00:00   123.4
1  2011-01-01 01:00:00  123.39  2012-01-01 01:00:00  123.38
2  2011-01-01 02:00:00   123.4  2012-01-01 02:00:00   123.4
3  2011-01-01 03:00:00   123.4  2012-01-01 03:00:00   123.4
4  2011-01-01 04:00:00  123.41  2012-01-01 04:00:00   123.4

Into the following shape:

reshaped_sample_df          
Out[97]:            
                      0       1
0   2011-01-01 00:00:00  123.39
1   2011-01-01 01:00:00  123.39
2   2011-01-01 02:00:00   123.4
3   2011-01-01 03:00:00   123.4
4   2011-01-01 04:00:00  123.41
5   2012-01-01 00:00:00   123.4
6   2012-01-01 01:00:00  123.38
7   2012-01-01 02:00:00   123.4
8   2012-01-01 03:00:00   123.4
9   2012-01-01 04:00:00   123.4

I've been struggling with plenty of the reshaping data functions in Pandas but without success.

CodePudding user response:

For a generic way, you can use pandas.concat in a loop:

out = pd.concat([df.iloc[:, x:x 2].set_axis([0, 1], axis=1)
                 for x in range(0, df.shape[1], 2)],
                ignore_index=True)

output:

                     0       1
0  2011-01-01 00:00:00  123.39
1  2011-01-01 01:00:00  123.39
2  2011-01-01 02:00:00  123.40
3  2011-01-01 03:00:00  123.40
4  2011-01-01 04:00:00  123.41
5  2012-01-01 00:00:00  123.40
6  2012-01-01 01:00:00  123.38
7  2012-01-01 02:00:00  123.40
8  2012-01-01 03:00:00  123.40
9  2012-01-01 04:00:00  123.40

CodePudding user response:

You can use pd.concat like this

part1 = df[[0, 1]]
part2 = df[[2, 3]].set_axis([0, 1], axis=1)

result = pd.concat([part1, part2], axis=0, ignore_index=True)

CodePudding user response:

Assuming you are reshaping in steps of 2, you can dump into numpy and rebuild the dataframe:

pd.DataFrame(np.reshape(df.to_numpy(), (-1,2)))

                     0       1
0  2011-01-01 00:00:00  123.39
1  2012-01-01 00:00:00   123.4
2  2011-01-01 01:00:00  123.39
3  2012-01-01 01:00:00  123.38
4  2011-01-01 02:00:00   123.4
5  2012-01-01 02:00:00   123.4
6  2011-01-01 03:00:00   123.4
7  2012-01-01 03:00:00   123.4
8  2011-01-01 04:00:00  123.41
9  2012-01-01 04:00:00   123.4
  • Related