如何按此优先级在DataFrame下方排序
1)“ Seller_id”升序
2)基于Custom_ordering = ['Jan','Feb','Mar','Apr','May']的“月”
3)'Sales_amount'降序
df = pd.DataFrame({
'Seller_id': [121,121,121,121,121,121,
321,321,321,321,321,321,
597,597,597,597,597,597],
'Months': ['Feb', 'Jan', 'Mar', 'May', 'Apr','Mar',
'Jan', 'Feb', 'Mar', 'Apr', 'May','Feb',
'Jan', 'Feb', 'Mar', 'Apr', 'May','Jan'],
'Sales_amount': [100,87,95,105,100,100,
100,87,95,105,110,105,
100,105,95,100,110,105]
})
Custom_ordering = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
Seller_id Months Sales_amount
0 121 Jan 87
1 121 Feb 100
2 121 Mar 100
3 121 Mar 95
4 121 Apr 100
5 121 May 105
6 321 Jan 100
7 321 Feb 105
8 321 Feb 87
9 321 Mar 95
10 321 Apr 105
11 321 May 110
12 597 Jan 105
13 597 Jan 100
14 597 Feb 105
15 597 Mar 95
16 597 Apr 100
17 597 May 110
答案 0 :(得分:1)
首先使用pd.Categorical将Month转换为分类:
df['Months'] = pd.Categorical(df['Months'], categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May'], ordered = False)
现在做:
df.sort_values(['Seller_id', 'Months', 'Sales_amount'], ascending = [True, True, False])