Python 按多索引中的级别子集对数据帧进行排序
我有以下数据帧:Python 按多索引中的级别子集对数据帧进行排序,python,pandas,dataframe,Python,Pandas,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', 'Wal
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'
排序,顺序为递减。您可以按特定索引级别使用,并指定排序是否应递增:
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