I have data arranged in long format, below.
Type Subtype Month Amount
A a Jan-21 7
A b Feb-21 9
A a Jan-21 8
A c Mar-21 2
C a Dec-20 7
I'm trying to reshape to wide and order the dates in the Month column in chronological order so it looks like this;
Month Dec-20 Jan-21 Feb -21 Mar-21
Type Subtype
A a 15
A b 9
A c 2
C a 7
I've tried using pd.pivot_table and also pd.groupby but they keep arranging the Months in alphabetical order. I have ensured the Month column is a datetime object.
This is my code (I've filled the NaN's with 0):
pivot = CF.groupby(['Type', 'Subtype',pd.Grouper(key ='Month')])['Amount'].sum().reset_index().pivot_table(index=['Type', 'Subtype'], columns='Month', values='Amount').fillna(0)
Any suggestions welcome. Thanks!