Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading