How to store a multidimensional matrix in a dataframe with pandas ?

Published: September 17, 2021

Tags: Python; Pandas; DataFrame;

DMCA.com Protection Status

Examples of how store a multidimensional matrix in a dataframe with pandas:

Create a 3D matrix with numpy

Let's consider the following 3D matrix with indexes i,j,k created with numpy

import numpy as np

data = 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 pd

iterables = [[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   2
i j            
0 0  63  35  74
  1  28  69  48
  2  25  19  33
1 0  51  23  28
  1  40  76   0
  2  10  48  82
2 0  51  55   7
  1  12  26  91
  2   1  73  31

References