x1 x1.resample('1T').mean
ts
2017-09-09 17:22:42 7.0 NaN
2017-09-09 17:22:53 11.0 NaN
2017-09-09 17:23:04 9.0 NaN
2017-09-09 17:23:15 15.0 NaN
2017-09-09 17:23:26 13.0 NaN
2017-09-09 17:23:38 19.0 NaN
2017-09-09 17:23:49 13.0 NaN
2017-09-09 17:24:00 15.0 10.666667
以上是df.x1Avg = df.x1.resample('1T').mean()
的结果该代码仅在 ts 以 hh:mm:00 结尾时返回结果。我想要的结果是
x1 x1.resample('1T').mean
ts
2017-09-09 17:22:42 7.0 (7+11)/2
2017-09-09 17:22:53 11.0 (7+11)/2
2017-09-09 17:23:04 9.0 (9+15+13+19+13)/5
2017-09-09 17:23:15 15.0 (9+15+13+19+13)/5
2017-09-09 17:23:26 13.0 (9+15+13+19+13)/5
2017-09-09 17:23:38 19.0 (9+15+13+19+13)/5
2017-09-09 17:23:49 13.0 (9+15+13+19+13)/5
2017-09-09 17:24:00 15.0 15
最佳答案
您可以使用变换
df.index = pd.to_datetime(df.index)
df['mean'] = df.resample('1T').transform('mean')
你得到了
x1 mean
ts
2017-09-09 17:22:42 7.0 9.0
2017-09-09 17:22:53 11.0 9.0
2017-09-09 17:23:04 9.0 13.8
2017-09-09 17:23:15 15.0 13.8
2017-09-09 17:23:26 13.0 13.8
2017-09-09 17:23:38 19.0 13.8
2017-09-09 17:23:49 13.0 13.8
2017-09-09 17:24:00 15.0 15.0
关于python - pandas 对不规则间隔时间数据进行重新采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46434346/