Examples of how store a multidimensional matrix in a dataframe with pandas:
Table of contents
Create a 3D matrix with numpy
Let's consider the following 3D matrix with indexes i,j,k created with numpy
import numpy as npdata = np.random.randint(100,size=(3,3,3))print(data)
gives for example
[[[63 35 74][28 69 48][25 19 33]][[51 23 28][40 76 0][10 48 82]][[51 55 7][12 26 91][ 1 73 31]]]
Note: to get the value coressponding to i = 1, j = 2 and k = 0, a solution is to do:
print(data[1,1,0])
returns here for instance
40
Store matrix in a dataframe
A solution to store a multidimensional matrix in a dataframe is to first reshape the initial matrix to a 2D matrix:
data = data.reshape(9,3)print(data)
gives
[[63 35 74][28 69 48][25 19 33][51 23 28][40 76 0][10 48 82][51 55 7][12 26 91][ 1 73 31]]
and to use pandas MultiIndex:
import pandas as pditerables = [[0,1,2], [0,1,2]]index = pd.MultiIndex.from_product(iterables, names=['i', "j"])df = pd.DataFrame(data=data, index=index, columns = [0,1,2])df = df.rename_axis("k", axis="columns")print(df)
returns then
k 0 1 2i j0 0 63 35 741 28 69 482 25 19 331 0 51 23 281 40 76 02 10 48 822 0 51 55 71 12 26 912 1 73 31
