2017-07-01 54 views
0

我是新的熊猫和取得了枢轴表用下面的代码:熊猫透视表重新索引轴线

my_pivot_table = pd.pivot_table(budData_join_tb_join_func_join_bud, 
       ['Budget','YTD','Balance', '% of Total'], 
       index = ['Function', 'Category'], aggfunc = sum) 

这使我这样的(部分图像)的表: enter image description here

它也有'Total of Total'栏。 我的目标是重新编制索引,例如第一个指数的顺序是:

row2_order = ['Instruction', 'Support Services', 'Executive Admin.', 
       'School Admin.', 'Business Services', 'Op. & Maint. Of Plant', 
       'Transportation', 'Benefits','Debt Service','Transfers'] 

而对于第二个索引,顺序应该是:

row1_order = ['Wages', 'Benefits', 'Property Service', 'Professional Services', 
       'Debt Service','Supplies','Other Services','Equipment', 
       'Dues & Fees', 'Transfer to Food Service'] 

所以,据我已经通过学会互联网,我写:

multi_index = [np.array(row1_order), np.array(row2_order)] 
my_pivot_table = my_pivot_table.reindex_axis(multi_index, axis = 0) 

但它变成这样(完整图像): enter image description here

应该发生的是,对于row1_order中的每个项目,row2_order中的所有项目都应该显示哪些值存在,如上一个表格中所示。 我在做什么错?任何帮助将不胜感激。

回答

0

终于解决了它,因为我想在row1_order各项指标,全部10项指标中row2_order,所以我把它们改变这些值:

row1_order = ['Instruction','Instruction','Instruction','Instruction','Instruction','Instruction','Instruction','Instruction','Instruction','Instruction', 
      'Support Services','Support Services','Support Services','Support Services','Support Services','Support Services','Support Services','Support Services','Support Services','Support Services', 
      'Executive Admin.','Executive Admin.','Executive Admin.','Executive Admin.','Executive Admin.','Executive Admin.','Executive Admin.','Executive Admin.','Executive Admin.','Executive Admin.', 
      'School Admin.','School Admin.','School Admin.','School Admin.','School Admin.','School Admin.','School Admin.','School Admin.','School Admin.','School Admin.', 
      'Business Services','Business Services','Business Services','Business Services','Business Services','Business Services','Business Services','Business Services','Business Services','Business Services', 
      'Op. & Maint. Of Plant','Op. & Maint. Of Plant','Op. & Maint. Of Plant','Op. & Maint. Of Plant','Op. & Maint. Of Plant','Op. & Maint. Of Plant','Op. & Maint. Of Plant','Op. & Maint. Of Plant','Op. & Maint. Of Plant','Op. & Maint. Of Plant', 
      'Transportation','Transportation','Transportation','Transportation','Transportation','Transportation','Transportation','Transportation','Transportation','Transportation', 
      'Benefits','Benefits','Benefits','Benefits','Benefits','Benefits','Benefits','Benefits','Benefits','Benefits', 
      'Debt Service','Debt Service','Debt Service','Debt Service','Debt Service','Debt Service','Debt Service','Debt Service','Debt Service','Debt Service', 
      'Transfers','Transfers','Transfers','Transfers','Transfers','Transfers','Transfers','Transfers','Transfers','Transfers'] 

row2_order = ['Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service', 
     'Wages', 'Benefits', 'Property Service', 'Professional Services', 
     'Debt Service','Supplies','Other Services','Equipment', 
     'Dues & Fees', 'Transfer to Food Service'] 

,然后运行 multi_index = [np.array(row1_order), np.array(row2_order)] my_pivot_table = my_pivot_table.reindex_axis(multi_index, axis = 0) 做了什么,我一直在寻找。