Sort a pandas's dataframe series by month name?

前端 未结 6 1724
无人及你
无人及你 2020-12-01 06:09

I have a Series object that has:

    date   price
    dec      12
    may      15
    apr      13
    ..

Problem statement:

6条回答
  •  情深已故
    2020-12-01 06:32

    You can add the numerical month value together with the name in the index (i.e "01 January"), do a sort then strip off the number:

    total=(df.groupby(df['date'].dt.strftime('%m %B'))['price'].mean()).sort_index()
    

    It may look sth like this:

    01 January  xxx
    02 February     yyy
    03 March    zzz
    04 April    ttt
    
     total.index = [ x.split()[1] for x in total.index ]
    
    January xxx
    February yyy
    March zzz
    April ttt
    

提交回复
热议问题