0
x = np.random.rand(100, 4, 4)
x

   [[[0.71, 0.65, 0.78, 0.85],
    [0.09, 0.43, 0.41, 0.28],
    [0.25, 0.12, 0.8 , 0.44],
    [0.42, 0.06, 0.63, 0.26]],
   ...,

   [[0.46, 0.86, 0.13, 0.51],
    [0.37, 0.2 , 0.79, 0.13],
    [0.39, 0.3 , 0.34, 0.77],
    [0.48, 0.71, 0.38, 0.39]]])

I want to convert this into a pandas data frame such that

x[0][:,0] is column1, 
x[1][:,0] is column2 
and so on.

I searched for this. However all the answers I could find use PD.panel which has been deprecated.

1
  • I could think of for-loop, but maybe with larger dimension could be very slow. Commented Aug 19, 2021 at 6:21

1 Answer 1

1

You can use hstack():

df=pd.DataFrame(np.hstack(x))

OR

use concatenate()

df=pd.DataFrame(np.concatenate(x,axis=1))

output of df:

      0          1            2      ...      397           398     399
0   0.095396    0.963393    0.218431 ...    0.369935    0.094349    0.536987
1   0.742115    0.641849    0.555946 ...    0.732853    0.946447    0.987616
2   0.825148    0.937934    0.013133 ...    0.595918    0.486560    0.816795
3   0.049911    0.214139    0.702705 ...    0.406167    0.872152    0.488658

Note: you can also use reshape() but it will messup the order:

df=pd.DataFrame(x.reshape(x.shape[-1],x.shape[0]*x.shape[-1]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.