Pandas fillna() with last week's value on entire dataframe for time-series data

Question:

I have some time-series data. I need to fill the nulls for certain calculations. I know I can use fillna() with the method=’ffill’ to impute the nulls with the previous value.

df.fillna(method='ffill')

I also know I can grab the previous weeks’ value for a specific column using np.where() and .shift(7):

df['col1'] = np.where(df.col1.isnull(), df.col1.shift(7), df.col1)

Is there any way to do this to the entire dataframe at once with .fillna()?

Asked By: Ragnar Lothbrok

||

Answers:

If I understand your question correctly, you want to fill NaNs with a value from 7 days ago.

In that case, just use

df = df.fillna(df.shift(7))

which will work for the entire dataframe in one go.

Answered By: PyRsquared
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.