'Convert n same-size, d-dimensional numpy arrays to a dataframe with d+n columns

I recently asked this question, about converting n 2-dimensional arrays to a dataframe with 2+n columns. The solution I got works perfectly well, but can not easily be generalized to higher dimensions.

In the more general case, the code would take as input a list of n same-size, d-dimensional numpy arrays, and d lists whose lengths correspond to the sizes of the arrays in the corresponding dimensions. The output is a pandas dataframe where each row corresponds to one position in the d-dimensional array. The first d comlumns contain the coordinates from the lists, and the next n columns contain the corresponding values from the n arrays. The linked question contains an example in 2D.

How would I go about generalizing the code to the d-dimensional case? I feel like this problem is related to the Pandas melt function, but I'm not sure how to make it work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source