2016-07-20 10 views
2

ich folgende Datenrahmen haben:Art Datenrahmen durch eine Teilmenge von Ebenen in einem 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) 

Das sieht so aus:

stores_g = stores.groupby(level=0) 

:

         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 

Wie ich die Gruppen sortieren von 'year' und 'month' mit decreasing bestellen.

Antwort

3

können Sie verwenden sort_index durch spezifische Indexniveaus und angeben, ob die Reihenfolge aufsteigend werden soll oder nicht:

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 
Verwandte Themen