0

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.

HRDSL
  • 711
  • 1
  • 5
  • 22
  • You are stating in your question: "I've tried some options..." Please, provide these options you tried. – Vsevolod Timchenko Sep 12 '19 at 13:42
  • 3
    The `sort_values` method is all you need. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html#pandas.DataFrame.sort_values. The `ascending` argument can be a list like `[True, False]` to signify ascending on the first column but not ascending on the second. so `df.sort_values(['page_id', 'date'], ascending=[True, False])` – Buckeye14Guy Sep 12 '19 at 13:43
  • 1
    This worked! I was grouping by page_id when it wasn't really necessary. Thank you very much :) – HRDSL Sep 12 '19 at 13:48
  • Try: reset_index(drop=True) – kantal Sep 12 '19 at 13:59

0 Answers0