从未堆叠的数据透视表重命名pandas列值

时间:2016-08-21 19:02:59

标签: python pandas rename

我有pandas pivot table merge2,如下所示:

   Site   TripDate       Volume    Early_Vol  Percent_Vol
0  024l 2004-12-02  1117.134948  1117.134948     0.000000
1  024l 2005-05-07   390.980708  1117.134948    -0.650015
2  024l 2006-10-07   321.110175  1117.134948    -0.712559
3  024l 2007-10-13   527.631767  1117.134948    -0.527692
4  024l 2008-02-02   597.165065  1117.134948    -0.465449

然后我将merge2分组到sub1

sub1 = merge2[['Site','TripDate','Percent_Vol']]
sub1= sub1.set_index(['Site','TripDate'])

sub1看起来像:

             Percent_Vol
Site TripDate               
024l 2004-12-02     0.000000
     2005-05-07    -0.650015
     2006-10-07    -0.712559
     2007-10-13    -0.527692
     2008-02-02    -0.465449

然后我将sub1展开到t中,看起来像:

           Percent_Vol                                                
Site              024l      029l      033l 035l_r    035l_s    041r_r   
TripDate                                                                
2004-06-01         NaN       NaN       NaN    NaN       NaN       NaN   
2004-11-13         NaN       NaN       NaN    NaN       NaN       NaN   
2004-12-02    0.000000  0.000000  0.000000    0.0  0.000000  0.000000   
2005-05-07   -0.650015 -0.290539 -0.300276   -1.0 -0.075511 -0.298801   
2006-10-07   -0.712559 -0.769020 -0.393304   -1.0 -0.520052 -0.284686  

t中的列名称(即Percent_VolSite)似乎有两个级别,而我只有Site。有没有办法将列名更改为Site

1 个答案:

答案 0 :(得分:1)

试试这个:

t.columns = t.columns.droplevel()