2017-07-01 68 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) 做了什麼,我一直在尋找。