I have a dataset where I would like to rearrange and sort quarter values in numerical order, grouping by the 'id' column
Data
id date stat
aa q1 22 y
aa q1 23 y
aa q2 22 y
aa q2 23 y
aa q3 22 y
aa q3 23 y
aa q4 22 y
aa q4 23 ok
bb q1 22 n
bb q1 23 n
bb q2 22 n
bb q2 23 n
bb q3 22 n
bb q3 23 n
bb q4 22 n
bb q4 23 ok
Desired
id date stat
aa q1 22 y
aa q2 22 y
aa q3 22 y
aa q4 22 y
aa q1 23 y
aa q2 23 y
aa q3 23 ok
aa q4 23 n
bb q1 22 n
bb q2 22 n
bb q3 22 n
bb q4 22 n
bb q1 23 n
bb q2 23 n
bb q3 23 n
bb q4 23 ok
Doing
Since my data is in quarters, I am using this
import pandas as pd
pd.to_datetime(date).sort_values().to_period('Q')
However, I also need to group these by the 'id' column as the desired output shows. Any suggestion is appreciated