[英]Sort aggregated column in multiindex dataframe within groups
從這里的數據中汲取靈感,我們有以下系列/數據框
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
在最新的 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.