code
code

Reputation: 13

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. How can I fill all the all the unfilled date values with corresponding date?

Following is the snippet of my data frame

Upvotes: 0

Views: 2691

Answers (2)

Pal
Pal

Reputation: 990

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')

Upvotes: 3

BENY
BENY

Reputation: 323396

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)

Upvotes: 3

Related Questions