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

Time:04-12

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()?

CodePudding user response:

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.

  • Related