I have a numpy
3d array. I'd like to create a pandas
dataframe off of it which would have as many rows as the array's 1st dimension's size and the number of columns would be the size of the 2nd dimension. The values of the dataframe should be the actual vectors (numpy
arrays) with the size of the third dimension.
Like if I have this array of size (2, 3, 5)
:
>>> arr
array([[[ 1., 1., 1., 1., 1.],
[ 0., 0., 0., 0., 0.],
[ 1., 2., 3., 4., 6.]],
[[ 0., 0., 0., 0., 0.],
[11., 22., 33., 44., 66.],
[ 0., 0., 0., 0., 0.]]])
I want to turn it into this dataframe (and do this efficiently with native numpy
and/or pandas
methods):
>>> df_arr
col0 col1 col2
0 [1.0, 1.0, 1.0, 1.0, 1.0] [0.0, 0.0, 0.0, 0.0, 0.0] [1.0, 2.0, 3.0, 4.0, 6.0]
1 [0.0, 0.0, 0.0, 0.0, 0.0] [11.0, 22.0, 33.0, 44.0, 66.0] [0.0, 0.0, 0.0, 0.0, 0.0]
CodePudding user response:
pd.DataFrame.from_records
works:
pd.DataFrame.from_records(arr, columns=['col0', 'col1', 'col2'])
col0 ... col2
0 [1.0, 1.0, 1.0, 1.0, 1.0] ... [1.0, 2.0, 3.0, 4.0, 6.0]
1 [0.0, 0.0, 0.0, 0.0, 0.0] ... [0.0, 0.0, 0.0, 0.0, 0.0]
[2 rows x 3 columns]