Moving Average- Pandas
<p>I would like to add a moving average calculation to my exchange time series.</p>

<p>Original data from Quandl</p>

<p>Exchange = Quandl.get("BUNDESBANK/BBEX3_D_SEK_USD_CA_AC_000", authtoken="xxxxxxx")</p>

<pre><code>    Value
Date               
1989-01-02  6.10500
1989-01-03  6.07500
1989-01-04  6.10750
1989-01-05  6.15250
1989-01-09  6.25500
1989-01-10  6.24250
1989-01-11  6.26250
1989-01-12  6.23250
1989-01-13  6.27750
1989-01-16  6.31250
</code></pre>

<h1>Calculating Moving Avarage</h1>

<p>MovingAverage = pd.rolling_mean(Exchange,5)</p>

<pre><code>              Value
Date          
1989-01-02      NaN
1989-01-03      NaN
1989-01-04      NaN
1989-01-05      NaN
1989-01-09  6.13900
1989-01-10  6.16650
1989-01-11  6.20400
1989-01-12  6.22900
1989-01-13  6.25400
1989-01-16  6.26550
</code></pre>

<p>I would like to add the calculated Moving Average as a new column to the right after 'Value' using the same index (Date). Preferably i would also like to rename the calculated moving average to 'MA'</p>

<p>The rolling mean returns a <code>Series</code> you only have to add it as a new column of your <code>DataFrame</code> (<code>MA</code>) as described below. </p>

<p>For information, the <code>rolling_mean</code> function has been deprecated in pandas newer versions. I have used the new method in my example, see below a quote from the pandas <a href="http://pandas.pydata.org/pandas-docs/stable/computation.html" rel="nofollow">documentation</a></p>

<blockquote>
  <p><strong>Warning</strong> Prior to version 0.18.0, <code>pd.rolling_*</code>, <code>pd.expanding_*</code>, and <code>pd.ewm*</code> were module level functions and are now deprecated. These are replaced by using the <code>Rolling</code>, <code>Expanding</code> and <code>EWM.</code> objects and a corresponding method call.</p>
</blockquote>

<pre><code>df['MA'] = df.rolling(window=5).mean()

print(df)
#             Value    MA
# Date                   
# 1989-01-02   6.11   NaN
# 1989-01-03   6.08   NaN
# 1989-01-04   6.11   NaN
# 1989-01-05   6.15   NaN
# 1989-01-09   6.25  6.14
# 1989-01-10   6.24  6.17
# 1989-01-11   6.26  6.20
# 1989-01-12   6.23  6.23
# 1989-01-13   6.28  6.25
# 1989-01-16   6.31  6.27
</code></pre>

python python-3.x pandas moving-average