1

If I have a dataframe which has date column as follows, here 2019Y1-11M represents data of 2019 from Jan to November, 2019Y1-10Mrepresents data of 2019 from Jan to October.

How can I sort them by setting 2018Y1-01M as first and 2019Y1-11M as last?

          date
0   2019Y1-11M
1   2019Y1-10M
2   2019Y1-09M
3   2019Y1-08M
4   2019Y1-07M
5   2019Y1-06M
6   2019Y1-05M
7   2019Y1-04M
8   2019Y1-03M
9   2019Y1-02M
10  2019Y1-01M
11  2018Y1-12M
12  2018Y1-11M
13  2018Y1-10M
14  2018Y1-09M
15  2018Y1-08M
16  2018Y1-07M
17  2018Y1-06M
18  2018Y1-05M
19  2018Y1-04M
20  2018Y1-03M
21  2018Y1-02M
22  2018Y1-01M

I think of splitting 2018Y1-01M into two columns 2018 and 01 then I can do it, just wonder if it's possible without this process. Thanks.

ah bon
  • 9,293
  • 12
  • 65
  • 148

1 Answers1

1

Use sort_values:

>>> df.sort_values(by = 'date')
          date
22  2018Y1-01M
21  2018Y1-02M
20  2018Y1-03M
19  2018Y1-04M
18  2018Y1-05M
17  2018Y1-06M
16  2018Y1-07M
15  2018Y1-08M
14  2018Y1-09M
13  2018Y1-10M
12  2018Y1-11M
11  2018Y1-12M
10  2019Y1-01M
9   2019Y1-02M
8   2019Y1-03M
7   2019Y1-04M
6   2019Y1-05M
5   2019Y1-06M
4   2019Y1-07M
3   2019Y1-08M
2   2019Y1-09M
1   2019Y1-10M
0   2019Y1-11M
Vicrobot
  • 3,795
  • 1
  • 17
  • 31