对组内多索引数据帧中的聚合列进行排序



从这里的数据中获得灵感,我们得到了以下系列/数据帧

df = data.groupby(["Manufacturer","Product Name","Product Launch Data"]).sum("total")

total
Manufacturer Product Name Product Launch Date       
Apple        iPad         2010-04-03              30
iPod         2001-10-23              34
Samsung      Galaxy       2009-04-27              24
Galaxy Tab   2010-09-02              22

我们如何在total之后排序,同时仍然保留组,即以结束

total
Manufacturer Product Name Product Launch Date       
Apple        iPad         2010-04-03              30
iPod         2001-10-23              34
Samsung      Galaxy Tab   2010-09-02              22
Galaxy       2009-04-27              24

在最近的panda版本中,可以同时按级别和列名进行排序,因此在这里工作:

df = df.sort_values(['Manufacturer','total'])
print (df)
total
Manufacturer Product Name Product Launch Date       
Apple        iPad         2010-04-03              30
iPod         2001-10-23              34
Samsung      Galaxy Tab   2010-09-02              22
Galaxy       2009-04-27              24

最新更新