How can I sort below DataFrame in this Priority order
1) 'Seller_id' in ascending
2) 'Months' on basis of Custom_ordering = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
3) 'Sales_amount' in descending
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
First convert Months to categorical using pd.Categorical :
df['Months'] = pd.Categorical(df['Months'], categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May'], ordered = False)
Now do:
df.sort_values(['Seller_id', 'Months', 'Sales_amount'], ascending = [True, True, False])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.