简体   繁体   English

如何从 Pandas dataframe 中的分钟数据中删除每日平均值

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

I have a pandas dataframe with a minute datetime index:我有一个带有分钟日期时间索引的 pandas dataframe:

Index指数 Col1列1
2022-12-25 09:01:00 2022-12-25 09:01:00 5 5个
2022-12-25 09:10:00 2022-12-25 09:10:00 15 15
2022-12-25 11:12:00 2022-12-25 11:12:00 10 10
2022-12-26 10:05:00 2022-12-26 10:05:00 2 2个
2022-12-26 12:29:00 2022-12-26 12:29:00 2 2个
2022-12-26 13:56:00 2022-12-26 13:56:00 5 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):我想从此数据中删除日平均值,结果为 dataframe(此处第一天为 10,第二天为 3):

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

Assuming df is your dataframe, this should do the trick:假设 df 是您的 dataframe,这应该可以解决问题:

for day, df_group in df.groupby(by=df.index.day):
    
    df.loc[df_group.index,"Col1"] -= df_group["Col1"].mean()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM