Edit This approach is much more elegant (and two orders of magnitude faster) than the one I gave below.
The original solution . Given a setting similar to that described above, but in 3-D,
from pandas import DataFrame, MultiIndex from itertools import product index = range(2), range(2), range(2) value = range(2 * 2 * 2) frame = DataFrame(value, columns=['value'], index=MultiIndex.from_product(index)).drop((1, 0, 1)) print(frame)
we have
value 0 0 0 0 1 1 1 0 2 1 3 1 0 0 4 1 0 6 1 7
Now we continue to use the reshape() route, but with some preprocessing to ensure that the length along each dimension is consistent.
First, flip the data frame with the full Cartesian product of all measurements. NaN values will be added as needed. This operation can be slow and consume a lot of memory depending on the number of measurements and the size of the data frame.
levels = map(tuple, frame.index.levels) index = list(product(*levels)) frame = frame.reindex(index) print(frame)
which outputs
value 0 0 0 0 1 1 1 0 2 1 3 1 0 0 4 1 NaN 1 0 6 1 7
Now reshape() will work as intended.
shape = map(len, frame.index.levels) print(frame.values.reshape(shape))
which outputs
[[[ 0. 1.] [ 2. 3.]] [[ 4. nan] [ 6. 7.]]]
(pretty ugly) single line
frame.reindex(list(product(*map(tuple, frame.index.levels)))).values\ .reshape(map(len, frame.index.levels))