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]]])

我想把它转换成一个熊猫数据框,这样

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

我搜索了这个。但是,我能找到的所有答案都使用已弃用的 PD.panel。

4

1 回答 1

1

您可以使用hstack()

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

或者

利用concatenate()

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

输出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

注意:您也可以使用reshape(),但它会打乱顺序:

df=pd.DataFrame(x.reshape(x.shape[-1],x.shape[0]*x.shape[-1]))
于 2021-08-19T06:22:34.567 回答