【发布时间】:2021-12-25 15:44:47
【问题描述】:
我有一个具有重复时间索引的数据框,我想获得前 2 天的平均值(我不想放弃任何观察;它们都是我需要的信息)。我检查了 pandas 文档并阅读了 Stackoverflow 上的先前帖子(例如 Apply rolling mean function on data frames with duplicated indices in pandas),但找不到解决方案。这是我的数据框的外观和我正在寻找的输出的示例。提前谢谢你。
数据:
import pandas as pd
df = pd.DataFrame({'id': [1,1,1,2,3,3,4,4,4],'t': [1, 2, 3, 2, 1, 2, 2, 3, 4],'v1':[1, 2, 3, 4, 5, 6, 7, 8, 9]})
输出:
| t | v2 |
|---|---|
| 1 | - |
| 2 | - |
| 3 | 4.167 |
| 4 | 5 |
| 5 | 6.667 |
【问题讨论】:
标签: python pandas rolling-computation