Nhyi
Nhyi

Reputation: 383

Filling In Empty Dates With Previous Data in Pandas

So I have the current file in Excel where I have dates and don't have dates for everything which can be seen.

enter image description here

I read this excel file into a pandas dataframe, rename the column and get the following:

enter image description here

My question is, how would I get it so every empty date in the dataframe is filled in with the last previous date encountered. All of the blanks between 04/03/2021 and 05/03/2021 gets replaced with 04/03/2021, so every row in my dataframe has a date associated with it?

Thanks!

Upvotes: 0

Views: 434

Answers (2)

Akhil Kurup
Akhil Kurup

Reputation: 137

After reading the data into a dataframe, you can fill missing values using fillna with method='ffill' for forward fill

Upvotes: 1

Nhyi
Nhyi

Reputation: 383

Just using the inbuilt way in pandas of:

duplicate_df['StartDate'] = duplicate_df['StartDate'].fillna(method = 'ffill')

This replaces all the NaNs in the dataframe with the last row that had data in.

Upvotes: 0

Related Questions