Home > Mobile >  sort strings of 'Apr-01',' Feb-02'... sort in January to December Order: Pandas
sort strings of 'Apr-01',' Feb-02'... sort in January to December Order: Pandas

Time:10-14

Folks, as always, all help is immensely appreciated I have a panda column containing strings from datetime obj. Need to sort them in January - December Order (Jan, Feb, Mar...Dec). I tried to convert the column to datetime but looks like Apr-01 without a year reference wouldn't convert (or i couldn't figure it out)

df = pd.DataFrame(['Apr-01','Apr-02','Apr-03','Aug-01','Aug-02',
      'Aug-03','Dec-07','Dec-08','Dec-09','Jul-01','Jul-02','Jul-03'])

CodePudding user response:

you could add a default year, e.g.

import pandas as pd

df = pd.DataFrame({'dates':['Apr-01','Apr-02','Apr-03','Aug-01','Aug-02',
      'Aug-03','Dec-07','Dec-08','Dec-09','Jul-01','Jul-02','Jul-03']})

# use 2020 as default year in case dates originate from leap year...
df['dates_y'] = pd.to_datetime(df['dates'] ' 2020')

df = df.sort_values(by=['dates_y'])

df
Out[2]: 
     dates    dates_y
0   Apr-01 2020-04-01
1   Apr-02 2020-04-02
2   Apr-03 2020-04-03
9   Jul-01 2020-07-01
10  Jul-02 2020-07-02
11  Jul-03 2020-07-03
3   Aug-01 2020-08-01
4   Aug-02 2020-08-02
5   Aug-03 2020-08-03
6   Dec-07 2020-12-07
7   Dec-08 2020-12-08
8   Dec-09 2020-12-09
  • Related