编辑:此问题已被标记为与how to find exponential weighted moving average using dataframe.ewma?重复
但尝试使用:
df1['ema'] = df1['values'].ewm(span=5)
如提到的帖子中所述,会出现以下错误:
NotImplementedError Traceback (most recent call last)
<ipython-input-8-5c535ef97a6c> in <module>
29
30 df1 = pd.DataFrame({'values': col_values})
---> 31 df1['ema'] = df1['values'].ewm(span=5)
…
c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\pandas\core\window.py in __iter__(self)
173 def __iter__(self):
174 url = 'https://github.com/pandas-dev/pandas/issues/11704'
--> 175 raise NotImplementedError('See issue #11704 {url}'.format(url=url))
176
177 def _get_index(self, index=None):
NotImplementedError: See issue #11704 https://github.com/pandas-dev/pandas/issues/11704
我试图在Pandas数据框中创建一个新列,其中包含名为“值”的列的指数移动平均值。
我尝试了以下方法:
import pandas as pd
import numpy as np
col_values = [-10.5,
15.899999999999864,
41.5,
65.19999999999993,
86.5,
96.10000000000014,
106.30000000000007,
110.5,
138.10000000000014,
104.50000000000034,
115.30000000000041,
122.80000000000041,
130.60000000000048,
146.50000000000034,
148.60000000000048,
173.00000000000057,
160.1000000000007,
185.00000000000057,
214.40000000000066,
241.80000000000052,
269.8000000000005,
286.6000000000006,
311.2000000000005,
323.80000000000064,
308.50000000000057]
df1 = pd.DataFrame({'values': col_values})
df1['ema'] = df1['values'].ewma(min_period=5)
df1.head(50)
但出现以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-d6630f195441> in <module>
29
30 df1 = pd.DataFrame({'values': col_values})
---> 31 df1['ema'] = df1['values'].ewma(min_period=5)
32 df1.head(50)
c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5065 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5066 return self[name]
-> 5067 return object.__getattribute__(self, name)
5068
5069 def __setattr__(self, name, value):
AttributeError: 'Series' object has no attribute 'ewma'