【问题标题】:Rename a multi index tuple pandas dataframe重命名多索引元组熊猫数据框
【发布时间】:2019-08-06 01:59:05
【问题描述】:

我有从 pd.pivot_table 发出的数据框

                          sum
                          Price
Manager       Status           
Debra Henley  declined    70000
              pending     50000
              presented   50000
              won         65000
Fred Anderson declined    65000
              pending      5000
              presented   45000
              won        172000

我想在最后一行的索引中添加一个 TOTAL 以获得这个结果:

                          sum
                          Price
Manager       Status           
Debra Henley  declined    70000
              pending     50000
              presented   50000
              won         65000
Fred Anderson declined    65000
              pending      5000
              presented   45000
              won        172000
All           TOTAL      522000

请问我该怎么做?

【问题讨论】:

  • 试试margins=True 到pivot_table。
  • 谢谢,但我想要的是在我的示例中填充“won”下和“All”前面的空白区域。你的建议行不通

标签: dataframe indexing multi-index


【解决方案1】:

例如这是数据框:

        Manager     Status      Price
0   Debra Henley    declined    1000
1   Fred Anderson   pending     1001
2   Debra Henley    presented   1002
3   Fred Anderson   won         1003
4   Debra Henley    declined    1004
5   Fred Anderson   pending     1005
6   Debra Henley    presented   1006
7   Fred Anderson   won         1007
8   Debra Henley    declined    1008
9   Fred Anderson   pending     1009
10  Debra Henley    presented   1010
11  Fred Anderson   won         1011
12  Debra Henley    declined    1012
13  Fred Anderson   declined    1013
14  Debra Henley    pending     1014
15  Fred Anderson   presented   1015
16  Debra Henley    won         1016
17  Fred Anderson   declined    1017
18  Debra Henley    declined    1018

旋转表格:

df.pivot_table(values='Price', index=['Manager', 'Status'], margins=True, margins_name='Total', aggfunc={'Price': np.sum})

结果:

                            Price
Manager         Status  
Debra Henley    declined    5042
                pending     1014
                presented   3018
                won         1016
Fred Anderson   declined    2030
                pending     3015
                presented   1015
                won         3021

要在最后一行添加 Total,请将此 margins=True, margins_name='Total' 添加到您的数据透视代码中。

最终代码:

df.pivot_table(values='Price', index=['Manager', 'Status'], margins=True, margins_name='Total', aggfunc={'Price': np.sum})

结果:

                            Price
Manager         Status  
Debra Henley    declined    5042
                pending     1014
                presented   3018
                won         1016
Fred Anderson   declined    2030
                pending     3015
                presented   1015
                won         3021
        Total              19171

希望有用

【讨论】:

  • 谢谢,但我想要在您的示例中填写“赢”下和“总计”前面的空白处
猜你喜欢
  • 2020-03-24
  • 2019-11-01
  • 2022-11-21
  • 2016-06-13
  • 2017-12-14
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 2020-09-04
相关资源
最近更新 更多