gpt4 book ai didi

python - 如何在 Pandas 中获得连续滚动平均值?

转载 作者:行者123 更新时间:2023-12-01 09:41:40 25 4
gpt4 key购买 nike

希望获得数据帧的连续滚动平均值。

df 看起来像这样

index price

0 4
1 6
2 10
3 12

希望获得持续的价格滚动

目标是让它看起来是所有价格的移动平均值。
index price  mean

0 4 4
1 6 5
2 10 6.67
3 12 8

先感谢您!

最佳答案

您可以使用扩展:

df['mean'] = df.price.expanding().mean()

df
index price mean
0 4 4.000000
1 6 5.000000
2 10 6.666667
3 12 8.000000

关于python - 如何在 Pandas 中获得连续滚动平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com