I have the following dataFrame:
page_id date
1 2018-01-01
1 2018-02-01
1 2018-03-01
2 2018-02-01
2 2018-03-01
3 2019-01-01
4 2019-04-01
4 2019-05-01
As you can see, the different page_ids are ordered ascendingly by date. What I want is to have them ordered descendengly, as follows:
page_id date
1 2018-03-01
1 2018-02-01
1 2018-01-01
2 2018-03-01
2 2018-02-01
3 2019-01-01
4 2019-05-01
4 2019-04-01
Note that the descending order is only among same page_ids, so sorting the whole dataframe wouldn't work, I first need to group by page_id. I've tried some options but I never get an output like the one I just showed.
Any tips on what would be the fastest way to do this will be highly appreciated.
EDIT: I've tried doing:
data = data.groupby('page_id').apply(lambda x: x.sort_values('date', ascending=False))
But Then I get an error when doing do reset_index(), so I don't get an output like the one I said is the expected output.