Month order Jan, Feb, Mar

When I list the months of the year by numbering 1, 2, 3… it presents the monthly order correctly, if I change the name of the months it doesn’t put the correct sequence of the months from January to December, they are mixed up.

How can I resolve this?

Hi, I used the forum search and here are two of the first results:

This is my code:

trace = df_anos.groupby(‘MES’)[‘TOTAL_OPERACAO’].sum().reset_index()

This is my result:

 MES  TOTAL_OPERACAO

0 ABR 180515.87550
1 AGO 224396.91270
2 DEZ 240666.38920
3 FEV 154843.35200
4 JAN 160208.75650
5 JUL 233122.57940
6 JUN 211895.01100
7 MAI 172629.32040
8 MAR 157203.49200
9 NOV 208175.99825
10 OUT 205776.35980
11 SET 228608.06500

This is the result I expected:
MES TOTAL_OPERACAO
0 JAN 160208.75650
1 FEV 154843.35200
2 MAR 157203.49200
3 ABR 180515.87550
4 MAI 172629.32040
5 JUN 211895.01100
6 JUL 233122.57940
7 AGO 224396.91270
8 SET 228608.06500
9 OUT 205776.35980
10 NOV 208175.99825
11 DEZ 240666.38920

I didn’t quite understand how to apply the answers that came up.

Hi,
It is better if you have columns in date format and use the above method.

However, if you have to manipulate month abbr, use the calendar library.

import calendar

df = df.sort_values(by='MES', key=lambda mes: {v:i for i,v in enumerate((calendar.month_abbr[1:]))}.get(mes))