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

How to remove daily mean from minute data in a Pandas dataframe

I have a pandas dataframe with a minute datetime index:

Index Col1
2022-12-25 09:01:00 5
2022-12-25 09:10:00 15
2022-12-25 11:12:00 10
2022-12-26 10:05:00 2
2022-12-26 12:29:00 2
2022-12-26 13:56:00 5

I want to remove the daily average from this data, resulting in this dataframe (here 10 for the first day and 3 for the second day):

Index Col1
2022-12-25 09:01:00 -5
2022-12-25 09:10:00 5
2022-12-25 11:12:00 0
2022-12-26 10:05:00 -1
2022-12-26 12:29:00 -1
2022-12-26 13:56:00 2

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 :

Assuming df is your dataframe, this should do the trick:

for day, df_group in df.groupby(by=df.index.day):
    
    df.loc[df_group.index,"Col1"] -= df_group["Col1"].mean()
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