pandas 计算每小时滚动平均值 [英] Pandas calculate hourly rolling mean

查看:97
本文介绍了 pandas 计算每小时滚动平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集 df 看起来像这样.这是一个基于 minute 的数据集.

My dataset df looks like this. It is a minute based dataset.

time, Open, High
2017-01-01 00:00:00, 1.2432, 1.1234
2017-01-01 00:01:00, 1.2432, 1.1234
2017-01-01 00:02:00, 1.2332, 1.1234
2017-01-01 00:03:00, 1.2132, 1.1234
...., ...., ....
2017-12-31 23:59:00, 1.2132, 1.1234

我想为 Open 列找到每小时 rolling mean 但应该灵活,以便我也可以找到其他的每小时 rolling mean列.

I want to find the hourly rolling mean for Open column but should be flexible so that I can also find hourly rolling mean for other columns.

我做了什么?

我能够找到如下所示的 每日滚动平均值,但是我如何找到以小时为基础的数据,以便我找不到一整天的 mean

I am able to find the daily rolling average like given below, but how do I find for the hour basis so that I do not find mean for the entire day

# Pandas code to find the rolling mean for a single day

df
.assign(1davg=df.rolling(window=1*24*60)['Open'].mean()) 
.groupby(df['time'].dt.date) 
.last() 

请注意,更改这行代码不起作用,因为我已经尝试过了:window=1*24*60window=60

Please note that changing this line of code does not work because I already tried it: window=1*24*60 to window=60

推荐答案

IIUC:

mask = (df["time"].dt.hour >= 22) | (df["time"].dt.hour <= 2)   
res = df.loc[mask].rolling("1H", on="time")["Open"].mean()

这篇关于 pandas 计算每小时滚动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆