It's best if you show results, and explain what's right or wrong. Don't expect us to "run the code" (in our heads or computer). Anyways, the first attempt:
In [18]: arr=np.random.randint(0, 5*3*4, (5, 3, 4))
In [19]: df= pd.DataFrame(arr.tolist(), columns=['A','B','C'])
In [20]: df
Out[20]:
A B C
0 [7, 5, 26, 14] [47, 46, 28, 45] [59, 19, 26, 46]
1 [8, 40, 52, 12] [37, 15, 52, 38] [38, 42, 19, 39]
2 [8, 51, 39, 53] [30, 53, 46, 34] [51, 30, 24, 16]
3 [21, 20, 37, 38] [39, 4, 37, 38] [51, 39, 39, 16]
4 [15, 11, 46, 46] [42, 56, 16, 5] [7, 9, 52, 26]
That's (5,3) frame, with 4 element lists in each cell. tolist
made a 3-level nested list.
Changing the array into (20,3), with the '3' as the last dimension:
In [21]: arr1 = arr.transpose(0,2,1).reshape(20,3); arr1
Out[21]:
array([[ 7, 47, 59],
[ 5, 46, 19],
[26, 28, 26],
[14, 45, 46],
...
[46, 16, 52],
[46, 5, 26]])
In [22]: df= pd.DataFrame(arr1, columns=['A','B','C'])
In [23]: df
Out[23]:
A B C
0 7 47 59
1 5 46 19
2 26 28 26
3 14 45 46
4 8 37 38
5 40 15 42
6 52 52 19
7 12 38 39
8 8 30 51
...
18 46 16 52
19 46 5 26
I'm not as good at pandas as numpy, but here's a way of assigning columns to a "blank" frame:
In [24]: df = pd.DataFrame(columns=['A','B','C'],dtype=int)
In [25]: df
Out[25]:
Empty DataFrame
Columns: [A, B, C]
Index: []
In [26]: df['A']=arr[:,0,:].ravel() # assign a (20,) array
In [27]: df['B']=arr[:,1,:].ravel()
In [28]: df['C']=arr[:,2,:].ravel()
In [29]: df
Out[29]:
A B C
0 7 47 59
1 5 46 19
2 26 28 26
3 14 45 46
....
While df
starts with 0 rows, after the column assignment it has full length:
In [32]: df
Out[32]:
A B C
0 7 NaN NaN
1 5 NaN NaN
2 26 NaN NaN
3 14 NaN NaN
4 8 NaN NaN
....
So as long as the number of rows is substantially larger than the number of columns, the one by one column assignment should be reasonably fast.