Efficiently Creating A Pandas DataFrame From A Numpy 3d array
<p>Suppose we start with</p>

<pre><code>import numpy as np
a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
</code></pre>

<p>How can this be efficiently be made into a pandas DataFrame equivalent to</p>

<pre><code>import pandas as pd
&gt;&gt;&gt; pd.DataFrame({'a': [0, 0, 1, 1], 'b': [1, 3, 5, 7], 'c': [2, 4, 6, 8]})

   a  b  c
0  0  1  2
1  0  3  4
2  1  5  6
3  1  7  8
</code></pre>

<p>The idea is to have the <code>a</code> column have the index in the first dimension in the original array, and the rest of the columns be a vertical concatenation of the 2d arrays in the latter two dimensions in the original array.</p>

<p>(This is easy to do with loops; the question is how to do it without them.)</p>

<hr>

<p><strong>Longer Example</strong></p>

<p>Using @Divakar's excellent suggestion:</p>

<pre><code>&gt;&gt;&gt; np.random.randint(0,9,(4,3,2))
array([[[0, 6],
    [6, 4],
    [3, 4]],

   [[5, 1],
    [1, 3],
    [6, 4]],

   [[8, 0],
    [2, 3],
    [3, 1]],

   [[2, 2],
    [0, 0],
    [6, 3]]])
</code></pre>

<p>Should be made to something like:</p>

<pre><code>&gt;&gt;&gt; pd.DataFrame({
    'a': [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], 
    'b': [0, 6, 3, 5, 1, 6, 8, 2, 3, 2, 0, 6], 
    'c': [6, 4, 4, 1, 3, 4, 0, 3, 1, 2, 0, 3]})
    a  b  c
0   0  0  6
1   0  6  4
2   0  3  4
3   1  5  1
4   1  1  3
5   1  6  4
6   2  8  0
7   2  2  3
8   2  3  1
9   3  2  2
10  3  0  0
11  3  6  3
</code></pre>