I am new to python, and I am trying to solve a problem sorting a df in month+year format, and my original data just looks like this: (re edit: Sorry, after checking the original df, the month column is actually like: Aug, Jul, Jul, Jun. Not number format)
ID year month ym
1 2018 Aug Aug 2018
2 2018 Jul Jul 2018
3 2019 Jul Jul 2019
4 2019 Jun Jun 2018
The way I figure out is ①split into two df according to year, and then ②sort month, finally ③merge them. But there're some problems:
for ①, I don't know how to do the split by groupby;
for ②, I tried to sort like this, it succeeds, but it looks like it's just temporarily sorted:
sort_order=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
df.index = pd.CategoricalIndex(df['month'], categories=sort_order, ordered=True)
df.sort_index().reset_index(drop=True)
for ③, I worry that because it's temporarily sorted, it will be the original one at the moment I try to merge they two.
I believe there should be better ways to solve this. Could anyone give a hint, or point out is there anything I got wrong? Thx!!