繁体   English   中英

通过多索引中的级别子集对数据框进行排序

[英]sort dataframe by a subset of levels in a multiindex

我有以下数据框:

data = {'year': [2010, 2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012, 2013],
                'store_number': ['1944', '1945', '1946', '1947', '1948', '1949', '1947', '1948', '1949', '1947'],
                'retailer_name': ['Walmart', 'Walmart', 'CRV', 'CRV', 'CRV', 'Walmart', 'Walmart', 'CRV', 'CRV', 'CRV'],
                'month': [1, 12, 3, 11, 10, 9, 5, 5, 4, 3],
                'amount': [5, 5, 8, 6, 1, 5, 10, 6, 12, 11]}

        stores = pd.DataFrame(data, columns=['retailer_name', 'store_number', 'year', 'month', 'amount'])
        stores.set_index(['retailer_name', 'store_number', 'year', 'month'], inplace=True)

看起来像:

                                       amount
retailer_name store_number year month        
Walmart       1944         2010 1           5
              1945         2010 12          5
CRV           1946         2011 3           8
              1947         2012 11          6
              1948         2011 10          1
Walmart       1949         2012 9           5
              1947         2010 5          10
CRV           1948         2011 5           6
              1949         2012 4          12
              1947         2013 3          11

我如何对组进行排序:

stores_g = stores.groupby(level=0)

'year''month'decreasing排列。

您可以按特定的索引级别使用sort_index ,并指定顺序是否应升序:

In [148]:
stores.sort_index(level=['year','month'], ascending=False)

Out[148]:
                                       amount
retailer_name store_number year month        
CRV           1947         2013 3          11
                           2012 11          6
Walmart       1949         2012 9           5
CRV           1949         2012 4          12
              1948         2011 10          1
                                5           6
              1946         2011 3           8
Walmart       1945         2010 12          5
              1947         2010 5          10
              1944         2010 1           5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM