【问题标题】:How to remove a level from the columns of a dataframe produced by pivot_table?如何从 pivot_table 生成的数据框的列中删除一个级别?
【发布时间】:2021-02-06 07:36:16
【问题描述】:

问题

我有一个类似于下面玩具示例的数据集。

我需要创建一个表格,将项目和期间的每个组合的值相加,并以交叉表/数据透视表格式显示。

如果我使用pandas.crosstab(),我会得到我想要的输出。

如果我使用 pandas.pivot_table,我会得到看起来像是多级索引的列。

如何去掉多级索引?

是的,我可以只使用crosstab,但是():

  1. 总的来说,我想了解多级索引
  2. 有时我 没有“原始”数据,我收到格式的数据 由 pivot_table 生成

我尝试过的

我试过totals_pivot_table.droplevel(0) 但它说只有一层。这是什么意思?

dataframe.columns.droplevel() 不再受支持

示例表

这是 pivot_table 的输出:

+--------+-------+-------+-------+-------+
|        | value | value | value | value |
+--------+-------+-------+-------+-------+
| period |     1 |     2 |     3 | All   |
| item   |       |       |       |       |
| x      |    10 |    11 |    12 | 33    |
| y      |    13 |    14 |    15 | 42    |
| All    |    23 |    25 |    27 | 75    |
+--------+-------+-------+-------+-------+

这是我需要的:

+------+----+----+----+-----+
| item | 1  | 2  | 3  | All |
+------+----+----+----+-----+
| x    | 10 | 11 | 12 |  33 |
| y    | 13 | 14 | 15 |  42 |
| All  | 23 | 25 | 27 |  75 |
+------+----+----+----+-----+

玩具代码

df = pd.DataFrame()

df['item'] = np.repeat(['x','y'],3)
df['period'] = np.tile([1,2,3],2)
df['value'] = np.arange(10,16)

pivot = df.pivot(index ='item', columns ='period', values = None)

totals_pivot_table = df.pivot_table(index ='item', columns = 'period', aggfunc ='sum', margins = True)

totals_ct = pd.crosstab( df['item'], df['period'], values =df['value'] , aggfunc ='sum', margins=True)

【问题讨论】:

    标签: python pandas dataframe pivot-table


    【解决方案1】:

    更好的是指定values参数:

    totals_pivot_table = df.pivot_table(index ='item', 
                                        columns = 'period', 
                                        values='value', 
                                        aggfunc ='sum', 
                                        margins=True)
    
    print (totals_pivot_table)
    period   1   2   3  All
    item                   
    x       10  11  12   33
    y       13  14  15   42
    All     23  25  27   75
    

    如果不可能,请使用DataFrame.droplevel,但要小心重复的列名:

    print (totals_pivot_table.droplevel(0, axis=1))
    period   1   2   3  All
    item                   
    x       10  11  12   33
    y       13  14  15   42
    All     23  25  27   75
    

    df = pd.DataFrame()
    
    df['item'] = np.repeat(['x','y'],3)
    df['period'] = np.tile([1,2,3],2)
    df['value'] = np.arange(10,16)
    df['value1'] = np.arange(7,13)
    print (df)
      item  period  value  value1
    0    x       1     10       7
    1    x       2     11       8
    2    x       3     12       9
    3    y       1     13      10
    4    y       2     14      11
    5    y       3     15      12
    
    totals_pivot_table = df.pivot_table(index ='item', 
                                        columns = 'period', 
                                        aggfunc ='sum', 
                                        margins=True)
    
    print (totals_pivot_table)
           value             value1            
    period     1   2   3 All      1   2   3 All
    item                                       
    x         10  11  12  33      7   8   9  24
    y         13  14  15  42     10  11  12  33
    All       23  25  27  75     17  19  21  57
    
    print (totals_pivot_table.droplevel(0, axis=1))
    period   1   2   3  All   1   2   3  All
    item                                    
    x       10  11  12   33   7   8   9   24
    y       13  14  15   42  10  11  12   33
    All     23  25  27   75  17  19  21   57
    

    【讨论】:

      【解决方案2】:

      使用 reset_index() 制作 df totals_ct

      totals_ct.index 
      

      给予:

      Index(['x', 'y', 'All'], dtype='object', name='item')
      

      但是,在制作 totals.ct 时使用 reset_index() 会删除所有三个索引

      totals_ct = pd.crosstab( df['item'], df['period'], values =df['value'] , aggfunc ='sum', margins=True).reset_index()
      

      检查结果:

      totals_ct.index
      

      给予:

      RangeIndex(start=0, stop=3, step=1)
      

      也许这就是你要找的。​​p>

      问候一月

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-05
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        相关资源
        最近更新 更多