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-20 3 2
2014-12-21 7 1
2014-12-22 2 9
2014-12-23 0 0
2014-12-24 2 2
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-20 3 2
2014-12-21 7 1
2014-12-22 2 9
2014-12-23 0 0
2014-12-24 2 2
Post a Comment for "Fill In Missing Days In Dataframe And Add Zero Value In Python"