我是pandas的新手,并使用以下代码创建了一个数据透视表:
my_pivot_table = pd.pivot_table(budData_join_tb_join_func_join_bud,
['Budget','YTD','Balance', '% of Total'],
index = ['Function', 'Category'], aggfunc = sum)
它占总数的百分比'专栏也是。 我的目标是重新索引索引,例如第一个索引的顺序是:
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)
应该发生的是,对于row1_order中的每个项目,应显示row2_order中存在哪些值的所有项目,如上一个表格中所示。 我究竟做错了什么?任何帮助将不胜感激。
答案 0 :(得分:0)
最后解决了它,因为我想要row1_order中的每个索引,row2_order中的所有10个索引,所以我将它们更改为这些值:
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)
做了我想要的。