简体   繁体   中英

Reshaping matrix in numpy

I am currently trying to reshape a triangular matrix into a different form like this

              S&P 500   Russel 2000     Nasdaq Composite
S&P 500             1             0     0
Russel 2000         4             5     0
Nasdaq Composite    7             8     9

    0                           1                  
0   S&P 500                     S&P 500      
1   Russel 2000                 S&P 500
2   Nasdaq Composite            S&P 500
3   S&P 500                     Russel 2000
4   Russel 2000                 Russel 2000
...

That is, I am trying to make repetitive columns for each index or vice versa.

I have used an array-oriented language called J, but I am pretty new to python numpy. Can you guys help me out on this problem?

Thanks in advance = ]

First of all this looks like a problem more easily tackled by pandas .

I have set something up which might help, but if you are really stuck with using Numpy then it won't be a proper solution to your problem.

First I have set up a similar DataFrame object to your numpy array:

$ import pandas as pd
$ df = pd.DataFrame(columns=['a','b','c'], index=['a','b','c'])
    a    b    c
a  NaN  NaN  NaN
b  NaN  NaN  NaN
c  NaN  NaN  NaN

Then I have filled it with the values you requested, although I don't use them.

$ df['a'] = [1, 4, 7]
$ df['b'] = [0, 5, 8]
$ df['c'] = [0, 0, 9]
   a  b  c
a  1  0  0
b  4  5  0
c  7  8  9

Finally I have simply taken the index and the columns and used the product function from itertools to get every combination into a list of tuples.

$ from itertools import product
$ p = product(df.columns.values, df.index.values)
$ new_df = pd.DataFrame([i for i in p])
   0  1
0  a  a
1  a  b
2  a  c
3  b  a
4  b  b
5  b  c
6  c  a
7  c  b
8  c  c

I would have a look at the Pandas documentation. They have many functions to manipulate dataframes in ways that you might be interested in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM