设置数据帧的垂直索引

时间:2017-12-06 04:07:53

标签: python python-3.x dataframe

我想将索引'Stkcd'带入垂直索引,以便每行显示1,2,3 ....行。我尝试了很多具有不同索引的转置,但我似乎索引总是转移到水平行,这不是我想要的。我希望我的结果是这样的。Stkcd on the vertical index on the left side

这是我得到的I don't want the Stkcd to be on the horizontal line原始数据是enter image description here

1 个答案:

答案 0 :(得分:0)

使用unstack

假设:

print(df1)
                  value
Stkcd date             
1     2015-12-02     11
      2015-12-03     12
      2015-12-04     12
      2015-12-05     11
      2015-12-06     10
      2015-12-07     12
      2015-12-08     10
      2015-12-09     10
      2015-12-10     11
      2015-12-11     14
      2015-12-12     14
      2015-12-13     12
      2015-12-14     14
2     2015-12-02     13
      2015-12-03     14
      2015-12-04     10
      2015-12-05     12
      2015-12-06     13
      2015-12-07     12
      2015-12-08     11
      2015-12-09     10
      2015-12-10     12
      2015-12-11     12
      2015-12-12     13
      2015-12-13     10
      2015-12-14     11
3     2015-12-02     10
      2015-12-03     11
      2015-12-04     13
      2015-12-05     11
      2015-12-06     12
      2015-12-07     10
      2015-12-08     12
      2015-12-09     11
      2015-12-10     14
      2015-12-11     13
      2015-12-12     12
      2015-12-13     11
      2015-12-14     12

df1.unstack()

输出:

           value                                                                               \
date  2015-12-02 2015-12-03 2015-12-04 2015-12-05 2015-12-06 2015-12-07 2015-12-08 2015-12-09   
Stkcd                                                                                           
1             11         12         12         11         10         12         10         10   
2             13         14         10         12         13         12         11         10   
3             10         11         13         11         12         10         12         11   


date  2015-12-10 2015-12-11 2015-12-12 2015-12-13 2015-12-14  
Stkcd                                                         
1             11         14         14         12         14  
2             12         12         13         10         11  
3             14         13         12         11         12