Skip to content Skip to sidebar Skip to footer

How To Fill Missing Dates In Pandas Dataframe?

MY DataFrame contains several data for each date. in my date column date has entered only for the first data of the day, for rest of the data of the day there is only sparse value.

Solution 1:

In Python

df['Date']=df['Date'].replace({'':np.nan}).ffill()

In R

library(zoo)
df$Date[df$Date=='']=NA
df$Date=na.locf(df$Date)

Solution 2:

You can use fillna function.

# Say df is your dataframe# To fill values forward use:
df.fillna(method='ffill') 

# To fill values backward use:
df.fillna(method='bfill')

Post a Comment for "How To Fill Missing Dates In Pandas Dataframe?"