Night Walker
Night Walker

Reputation: 21280

sort dataframe by a subset of levels in a multiindex

I have following dataframe:

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)

That looks like:

                                       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

How I can sort the groups:

stores_g = stores.groupby(level=0)

by 'year' and 'month' with decreasing order .

Upvotes: 2

Views: 282

Answers (1)

EdChum
EdChum

Reputation: 394159

You can use sort_index by specific index levels and specify whether the ordering should be ascending or not:

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

Upvotes: 3

Related Questions