【问题标题】:Pandas Pivot Table reindexing axis熊猫数据透视表重新索引轴
【发布时间】:2017-07-02 06:13:44
【问题描述】:

我是 pandas 的新手,并使用以下代码制作了一个数据透视表:

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

这给了我一个像这样的表格(部分图​​片):

它也有 '% 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)

但它变成了这样(全图):

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

【问题讨论】:

    标签: pandas pivot-table


    【解决方案1】:

    终于解决了,因为我想要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) 做了我想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 2021-02-28
      • 2023-01-11
      • 1970-01-01
      • 2016-06-13
      • 2012-12-31
      相关资源
      最近更新 更多