Python - 尝试计算时间 t 之前事件的平均时间

标签 python pandas rolling-average

这是一个示例数据集:

ID  Date
1   2/3/18
1   2/7/18
1   2/14/18
1   2/16/18

这是最终功能的样子:

ID  Date     Running_Mean
1   2/3/18   0
1   2/7/18   4
1   2/14/18  5.5
1   2/16/18  4.33

这是一个滚动窗口,从序列的开头开始,并随着数据集继续扩展。

任何帮助将不胜感激。

最佳答案

windows = len(df) 时,通过使用 expandingrolling 相同的东西

df.Date=pd.to_datetime(df.Date)

df.Date.diff().dt.days.expanding(1).mean()
Out[654]:
0         NaN
1    4.000000
2    5.500000
3    4.333333
Name: Date, dtype: float64

关于Python - 尝试计算时间 t 之前事件的平均时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51995863/

相关文章:

python - Django channel

python - Pandas + sklearn 线性回归失败

python - 尽管安装了 anaconda 解释器和 torch ,但无法在 vscode 中导入 torch

python - cars.iloc[[3, 0]]、cars.iloc[[3]、[0]] 和 cars.iloc[3, 0] 之间的区别

python - 两个相似形状之间的 OpenCV 形状匹配

python - 将数据帧行值和列标题打印为字符串

python - Pandas :沿着多索引的第一层切片

python - 找到等于的运行平均值

python - pandas groupby 对象中 n 和 n+1 行之间的平均值?