我有以下数据框:
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
订单。
答案 0 :(得分:3)
您可以按特定索引级别使用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