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

DataFrame: Get the mean of two rows to a set it to a row

I have a df like,

             ds             value
====================================
1 2023-07-01 17:00:00        100
2 2023-07-01 18:00:00         0
3 2023-07-01 19:00:00        300

I want to set the mean of 1st and 3rd rows value into the 2nd row like this

             ds             value
====================================
1 2023-07-01 17:00:00       100
2 2023-07-01 18:00:00       150
3 2023-07-01 19:00:00       200

And some times it may be more that one rows which will be having 0, is there a way to handle that dynamically.

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

Any help is much appreciated

Thanks,

>Solution :

IIUC use Series.replace with Series.interpolate:

df['value'] = df['value'].replace(0, np.nan).interpolate()
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