[英]Sort aggregated column in multiindex dataframe within groups
Taking inspiration from the data here we have the following Series/Dataframe从这里的数据中汲取灵感,我们有以下系列/数据框
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
How do we sort after total
while still keeping the groups ie ending up with:我们如何在
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
In last pandas versions is possible sorting by levels and columns names together, so here working:在最新的 Pandas 版本中,可以按级别和列名一起排序,所以这里工作:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.