0

I am struggling with pandas transformation, that I would expect to be quite easy. I have the following 2D (10 x 6) data frame:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,6), columns=['A1', 'A2', 'A3', 'B1', 'B2', 'B3'])

I would like to have a corresponding 3D (3 x 2 x 10) data frame, where instead of 1D row from above (A1, ..., B3), there is a 2D new DF:

A1 A2 A3
B1 B2 B3

Is this achievable in few lines?

mattino
  • 95
  • 7
  • Does this answer your question? [Reshaping a dataframe in python into 3D](https://stackoverflow.com/questions/50711786/reshaping-a-dataframe-in-python-into-3d) – Bill Huang Nov 22 '20 at 00:39

1 Answers1

2

In earlier versions of Pandas there existed Panel, i.e. a 3-D array, but the current version of Pandas does not contain it any more.

But you can create a Numpy 3-D array:

arr = df.values.reshape(3, 2, -1)
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41