Skip to content Skip to sidebar Skip to footer

Fill In Missing Days In Dataframe And Add Zero Value In Python

I have a dataframe that looks like the following Date A B 2014-12-20 00:00:00.000 3 2 2014-12-21 00:00:00.000 7 1 2014-12-22 00:

Solution 1:

If Date is column create DatetimeIndex and then use DataFrame.asfreq:

df['Date'] = pd.to_datetime(df['Date'])
df1 = df.set_index('Date').asfreq('d', fill_value=0)print (df1)
            A  B
Date            
2014-12-20322014-12-21712014-12-22292014-12-23002014-12-2422

If first column is index:

df.index = pd.to_datetime(df.index)
df1 = df.asfreq('d', fill_value=0)print (df1)
            A  B
Date            
2014-12-20322014-12-21712014-12-22292014-12-23002014-12-2422

Post a Comment for "Fill In Missing Days In Dataframe And Add Zero Value In Python"