如何获得大 pandas 的连续滚动平均值? [英] how to get a continuous rolling mean in pandas?

查看:37
本文介绍了如何获得大 pandas 的连续滚动平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Looking to get a continuous rolling mean of a dataframe.

df看起来像这样

index price

0      4
1      6
2      10
3      12

希望获得连续的价格

目标是让它看起来像所有价格的移动平均值.

the goal is to have it look this a moving mean of all the prices.

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

这篇关于如何获得大 pandas 的连续滚动平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆