0

I have two matrix: numpy square matrix and a panda multiindexed square matrix. They are the same size. The idea is to get the value from numpy into the multiindex panda matrix to navigate more easily into the data.

My matrix are around 100 000 x 100 000. And my panda matrix has three level of index.

tuples = [('1','A','a'), ('1','A','b'), ('1','A','c'), ('1','B','a'), ('1','B','b'), ('1','B','c'), ('2','A','a'), ('2','A','b'), ('2','B','a')]
index = pd.MultiIndex.from_tuples(tuples, names=['geography', 'product','activity'])
df = pd.DataFrame(index=index, columns=index)

geography                   1                 2      
product                     A        B        A     B
activity                    a  b  c  a  b  c  a  b  a
geography product activity                           
1         A       a         0  0  0  0  0  0  0  0  0
                  b         0  0  0  0  0  0  0  0  0
                  c         0  0  0  0  0  0  0  0  0
          B       a         0  0  0  0  0  0  0  0  0
                  b         0  0  0  0  0  0  0  0  0
                  c         0  0  0  0  0  0  0  0  0
2         A       a         0  0  0  0  0  0  0  0  0
                  b         0  0  0  0  0  0  0  0  0
          B       a         0  0  0  0  0  0  0  0  0

np.random.rand(9,9)
array([[ 0.27302806,  0.33926193,  0.01489047,  0.71959889,  0.43500806,
         0.03607795,  0.03747561,  0.43000199,  0.8091691 ],
       [ 0.96626878,  0.37613022,  0.7739084 ,  0.16724657,  0.01144436,
         0.0107722 ,  0.73513494,  0.13305542,  0.2910334 ],
       [ 0.00622779,  0.93699165,  0.62725798,  0.25009469,  0.14010666,
         0.61826728,  0.72060106,  0.58864557,  0.29375779],
       [ 0.14937979,  0.45269751,  0.68450964,  0.15986812,  0.69879559,
         0.06573519,  0.57504452,  0.49540882,  0.77283616],
       [ 0.60933817,  0.2701683 ,  0.69067959,  0.22806386,  0.79456502,
         0.75107457,  0.2805325 ,  0.27659171,  0.33446821],
       [ 0.82860687,  0.27055835,  0.37684942,  0.18962783,  0.59885119,
         0.31246936,  0.94522335,  0.53487273,  0.00611481],
       [ 0.27683582,  0.23653112,  0.41250374,  0.5024068 ,  0.27621212,
         0.81379001,  0.6704781 ,  0.87521485,  0.04577144],
       [ 0.95516958,  0.21844023,  0.86558273,  0.52300142,  0.91328259,
         0.7587479 ,  0.15201837,  0.15376074,  0.12092142],
       [ 0.36835891,  0.0381736 ,  0.36473176,  0.30510363,  0.19433639,
         0.43431018,  0.00112607,  0.35334684,  0.82307449]])

How I can put the value of the numpy matrix into in the panda multiindex matrix. The two matrix by construction have the same structure, i.e. the numpy matrix is the panda one without label indexes.

I found a dozen of examples to transform multiindex df into numpy array, but not in this way. Only one example of a 3 dimensional numpy array, but mine is not a 3-d np array.

4
  • How about df[:] = np.random.rand(9,9)? Commented Apr 25, 2017 at 9:07
  • SHAME on me !! Yes, perfectly. I was looking for far more complex. Commented Apr 25, 2017 at 9:13
  • Make sure the data order is as expected though. Commented Apr 25, 2017 at 9:13
  • Yes it is. The bumpy matrix is an inverted matrix from a numpy matrix created from the panda structure. So everything is right. Commented Apr 25, 2017 at 9:21

1 Answer 1

1

Thanks to Divakar. Something, just df[:] = np.random.rand(9,9) and it is all right.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.