按边距(“全部")值列对Pandas数据透视表进行排序 [英] Sort Pandas Pivot Table by the margin ('All') values column

查看:287
本文介绍了按边距(“全部")值列对Pandas数据透视表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过熊猫数据透视表中的行总和对最后一列/页边距/aggrfunc进行降序排序.我知道我在这里缺少一些简单的东西,但是我无法弄清楚.

I'm trying to do a descending sort on the last column/margins/aggrfunc by the sum of the rows in a pandas pivot table. I know I'm missing something simple here, but I can't figure it out.

数据框/数据透视表:

WIDGETS         
DATE    2/1/16  2/2/16  2/3/16  All
NAME                
PERSON1 43      5               48
PERSON2         4       7       11
PERSON3         56      143     199

我需要做的也是按aggfunc/margins排序:

What I need it to do is also sort by aggfunc/margins:

WIDGETS         
DATE    2/1/16  2/2/16  2/3/16  All
NAME                
PERSON3         56      143     199
PERSON1 43      5               48
PERSON2         4       7       11

pt = pd.pivot_table(df,values=['WIDGETS'],index=['NAME'],columns=['DATE'],aggfunc=len,fill_value='',margins=True,margins_name='WIDGETS')
pt.sort_values(by='WIDGETS',ascending=False,inplace=True)

错误: ValueError:无法在您需要明确提供所有级别的多索引中按列WIDGETS排序

Error: ValueError: Cannot sort by column WIDGETS in a multi-index you need to explicity provide all the levels

建议?

推荐答案

您可以在函数 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆