简体   繁体   English

大熊猫 - 指数加权移动平均线 - 类似于excel

[英]pandas - exponentially weighted moving average - similar to excel

Consider I've a dataframe of 10 rows having two columns A and B as following : 考虑我有一个包含两列A和B的10行数据帧,如下所示:

    A  B
0  21  6
1  87  0
2  87  0
3  25  0
4  25  0
5  14  0
6  79  0
7  70  0
8  54  0
9  35  0

In excel I can calculate the rolling mean like this excluding the first row: 在excel中我可以计算这样的rolling mean不包括第一行: 在此输入图像描述 在此输入图像描述

How can I do this in pandas? 我怎么能在熊猫中做到这一点?

Here is what I've tried: 这是我尝试过的:

import pandas as pd

df = pd.read_clipboard() #copying the dataframe given above and calling read_clipboard will get the df populated
for i in range(1, len(df)):
    df.loc[i, 'B'] = df[['A', 'B']].loc[i-1].mean()

This gives me the desired result matching excel. 这给了我匹配excel的理想结果。 But is there a better pandas way to do it? 但有没有更好的熊猫方式呢? I've tried using expanding and rolling did not produce desired result. 我尝试过使用expandingrolling没有产生预期的结果。

You have an exponentially weighted moving average, rather than a simple moving average. 您有一个指数加权移动平均线,而不是一个简单的移动平均线。 That's why pd.DataFrame.rolling didn't work. 这就是为什么pd.DataFrame.rolling不起作用的原因。 You might be looking for pd.DataFrame.ewm instead. 您可能正在寻找pd.DataFrame.ewm

Starting from 从...开始

df

Out[399]: 
    A  B
0  21  6
1  87  0
2  87  0
3  25  0
4  25  0
5  14  0
6  79  0
7  70  0
8  54  0
9  35  0

df['B'] = df["A"].shift().fillna(df["B"]).ewm(com=1, adjust=False).mean()
df

Out[401]: 
    A          B
0  21   6.000000
1  87  13.500000
2  87  50.250000
3  25  68.625000
4  25  46.812500
5  14  35.906250
6  79  24.953125
7  70  51.976562
8  54  60.988281
9  35  57.494141

Even on just ten rows, doing it this way speeds up the code by about a factor of 10 with %timeit (959 microseconds from 10.3ms). 即使只有十行,这样做也可以使用%timeit (从10.3ms开始959微秒)将代码加速大约10倍。 On 100 rows, this becomes a factor of 100 (1.1ms vs 110ms). 在100行上,这成为100的因子(1.1ms对110ms)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫指数加权移动平均线的问题 - Issue with pandas exponentially weighted moving average Pandas 在固定时间窗口内呈指数加权移动平均线 - Pandas exponentially weighted moving average over fixed time window Tradingview pinescript 在 python、pandas 中的 RMA(RSI 中使用的移动平均线。它是指数加权移动平均线,alpha = 1 / 长度) - Tradingview pinescript's RMA (Moving average used in RSI. It is the exponentially weighted moving average with alpha = 1 / length) in python, pandas 滚动加权移动平均线 pandas - rolling weighted moving average pandas 熊猫的指数加权函数:这些是移动窗口还是扩展窗口? - Pandas exponentially weighted functions: are these moving window or expanding window? 最近日期以熊猫为单位的新近度加权移动平均值 - Recency weighted moving average on previous dates in pandas 使用Pandas的指数加权移动平均线 - Exponential Weighted Moving Average using Pandas 熊猫的加权移动平均线-难以实施 - weighted moving average in pandas - having trouble implementing 指数加权平均值列中的最终值 - final value in column of exponentially weighted average values 使用熊猫以指数加权的移动std作为具有固定窗口大小的滑动窗口 - Using pandas Exponentially-weighted moving std as a sliding window with *fixed* window size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM