简体   繁体   English

Pandas:每分钟仅更新一秒数据的滚动平均值

[英]Pandas: Updating a rolling average only every minute for one second data

I have a dataframe where rows of data are in one second intervals, so 08:00:00, 08:00:01, etc. I want to take a rolling average over a period of 10 minutes, but I only want the rolling average to update on a minute by minute basis.我有一个 dataframe,其中数据行以一秒为间隔,所以 08:00:00、08:00:01 等。我想在 10 分钟内取一个滚动平均值,但我只想要滚动平均值每分钟更新一次。 So the rolling average values for 08:10:00 - 08:10:59 would all be the same value, and then at 8:11:00, it would update to a new value for the next minute.因此 08:10:00 - 08:10:59 的滚动平均值都将是相同的值,然后在 8:11:00,它将在下一分钟更新为新值。

Currently I'm using the following line to calculate a rolling average which updates every second:目前我正在使用以下行来计算每秒更新的滚动平均值:

df['counts-avg'] = df['counts'].rolling(window=600).mean()

I have another column for the seconds value called df['sec'].我还有一个名为 df['sec'] 的秒值列。 I got the indices of rows where seconds = 0 (the zeroth second of each minute) and replaced every other row with np.nan.我得到了 seconds = 0(每分钟的第 0 秒)的行的索引,并用 np.nan 替换了每隔一行。 Then I used fillna(method='ffill') to copy values downward.然后我使用 fillna(method='ffill') 向下复制值。

df['counts-avg'] = df['counts'].rolling(window=600).mean()
erase_idx = df[df['sec'] > 0].index.values
ma = df['counts-avg']
ma.loc[erase_idx] = np.nan
ma = ma.fillna(method='ffill')

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

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