How to recover original indices for a flattened Numpy array?

后端 未结 6 414
逝去的感伤
逝去的感伤 2021-01-02 19:49

I\'ve got a multidimensional numpy array that I\'m trying to stick into a pandas data frame. I\'d like to flatten the array, and create a pandas index that reflects the pre-

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 20:27

    Another possibility, although others maybe faster...

    x,y,z = np.indices(A.shape)
    
    df = pd.DataFrame(np.array([p.flatten() for p in [x,y,z,A]]).T
                      ,columns=['x','y','z',0])
    

提交回复
热议问题