How to recover original indices for a flattened Numpy array?

后端 未结 6 502
逝去的感伤
逝去的感伤 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:50

    def ndarray_to_indexed_2d(data):
        idx = np.column_stack(np.unravel_index(np.arange(np.product(data.shape[:-1])), data.shape[:-1]))
        data2d = np.hstack((idx, data.reshape(np.product(data.shape[:-1]), data.shape[-1])))
        return data2d
    

提交回复
热议问题