Python Panda dataframe sorting with month - year

后端 未结 1 524
轻奢々
轻奢々 2021-01-25 18:51

I am a python and pandas beginner and I\'m having trouble sorting a data frame after a groupby operation. I can get sorted data after groupby and count but when I put all the da

1条回答
  •  清酒与你
    2021-01-25 19:17

    One simple way is to just convert the index to date, sort and then convert back to month-year.

    result.index = pd.to_datetime(result.index)
    result.sort_index(inplace=True)
    result.index = monthly_transpose.index.strftime('%B-%Y')
    

    When converting to date, pandas converts the month-year to the first date of each month nad hence the sorting is made possible. Hope it helps.

    0 讨论(0)
提交回复
热议问题