我有一個'Date'
列的數據框。我想將它變成一個分類列,其中包括所有月份Jan
到Dec
。但是,我的專欄很多都沒有代表所有月份。用不完整的月份創建月份名稱的分類列的最佳方法
考慮數據框df
df = pd.DataFrame(dict(Date=pd.date_range('2011-03-31', periods=4, freq='Q')))
df
Date
0 2011-03-31
1 2011-06-30
2 2011-09-30
3 2011-12-31
我已經試過
df.Date.dt.strftime('%b').astype('category')
0 Mar
1 Jun
2 Sep
3 Dec
Name: Date, dtype: category
Categories (4, object): [Dec, Jun, Mar, Sep]
你可以看到,只有在我的專欄四個月表示爲類別。我如何去
0 Mar
1 Jun
2 Sep
3 Dec
Name: Date, dtype: category
Categories (12, object): [Jan, Feb, Mar, Apr, ..., Sep, Oct, Nov, Dec]