Stacking Columns one under another as a new dataframe

Viewed 147

I have stock data consisting of 999 close returns. I need to stack these 999 columns one under another using python i.e. stock 1, stock 2, ..., stock 999 all one under another.

1 Answers

I guess what you are looking for is unstack():

See the example below:

In [330]: df
Out[330]: 
  col1  col2  rnk
0    a     5  1.5
1    a     5  1.5
2    b    10  1.0
3    b    15  2.0
4    c    20  1.0

In [329]: df.unstack()
Out[329]: 
col1  0      a
      1      a
      2      b
      3      b
      4      c
col2  0      5
      1      5
      2     10
      3     15
      4     20
rnk   0    1.5
      1    1.5
      2      1
      3      2
      4      1
dtype: object

All 3 columns (col1,col2,rnk) are stacked below one another. Let me know if this is what you want.

Related