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

September 17, 2021    /    Viewed: 185    /    Comments: 0    /    Edit


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


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!