0

In R, you can use a melt function to take the below example table and convert it to a melted covariance table. Is there a function to perform this? I looked at pd.melt() but is there a way to perform this based on column name and row name? I am struggling to understand how to use this with column and row names.

ALL Examples below are made up for illustrations sake Start:

Stock AAPL COM  TTD
AAPL  1     .6   .5
COM   .6     1   .3
TTD   .5    .3    1

Finish:
AAPL AAPL  1
AAPL COM   .6
AAPL TTD  .5
COM AAPL  .6
COM COM   1
COMM TDD  .3
TTD AAPL  .5
TTD COM  .3
TTD TTD  1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • You want something like `df.melt('Stock')`? You need to specify the `id_vars` so that it knows what to consider the index. Alternatively you could `df.set_index('Stock').stack()`, the two are basically the same, just ordered differently and handle `NaN` slightly differently (`stack` drops them) – ALollz Aug 17 '21 at 17:54
  • for the id_vars technique how would I tell it to us the row names and header names? end goal is a df that is headered "stock 1, stock 2, cov" – Patrick McKercher Aug 17 '21 at 20:11

0 Answers0