从“未堆叠的数据透视”选项卡重命名pandas列值

2024-09-25 08:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有pandas pivot表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

^{pr2}$

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_Vol和{})中,我的列名有两个级别,而我只有Site。有没有办法把列名改成Site?在


Tags: pandassitenan级别pivotpercentvolume办法