-3

I have a pandas dataframe as shown below where I have Month-Year, need to get the continuous dataframe which should include count as 0 if no rows are found for that month. Excepcted output is shown below.

Input dataframe

Month   | Count
--------------
Jan-15  | 10
Feb-15  | 100
Mar-15  | 20
Jul-15  | 10
Sep-15  | 11 
Oct-15  | 1 
Dec-15  | 15

Expected Output

Month   | Count
--------------
Jan-15  | 10
Feb-15  | 100
Mar-15  | 20
Apr-15  | 0
May-15  | 0
Jun-15  | 0
Jul-15  | 10
Aug-15  | 0
Sep-15  | 11 
Oct-15  | 1
Nov-15  | 0 
Dec-15  | 15
Vinay Ranjan
  • 294
  • 3
  • 14

1 Answers1

4

You can set the Month column as the index. It looks like Excel input, if so, it will be parsed at 01.01.2015 so you can resample it as follows:

df.set_index('Month').resample('MS').asfreq().fillna(0)
Out: 
            Count
Month            
2015-01-01   10.0
2015-02-01  100.0
2015-03-01   20.0
2015-04-01    0.0
2015-05-01    0.0
2015-06-01    0.0
2015-07-01   10.0
2015-08-01    0.0
2015-09-01   11.0
2015-10-01    1.0
2015-11-01    0.0
2015-12-01   15.0

If the month column is not recognized as date, you need to convert it first:

df['Month'] = pd.to_datetime(df['Month'], format='%b-%y')
ayhan
  • 70,170
  • 20
  • 182
  • 203