title: Pandas - directly add moving average columns from group by to dataframe
body: <p>I have a dataframe with the following columns:</p>
  
  <pre><code>name, date, day_index, value
  </code></pre>
  
  <p>I want to add a 4th column to the same dataframe which is the exponentially weighted moving average of the 3rd column (value) for each name, sorted by first date and then day_index. I can generate this as a series using the following code.</p>
  
  <pre><code>df.sort_values(['date','day_index'],inplace=True)
  
  ecw_series = df.groupby('name').apply(lambda x: 
  x["value"].ewm(halflife=2).mean())
  </code></pre>
  
  <p>However, if I try and directly add this to the original dataframe, I get the following error:</p>
  
  <pre><code>df['ecw'] =  df.groupby('name').apply(lambda x: 
  x["value"].ewm(halflife=2).mean())
  
  
  
  incompatible index of inserted column with frame index
  </code></pre>
  
  <p>And if I try and merge the series with the dataframe I get the following error:</p>
  
  <pre><code>df['index'] = df.index
  
  df = df.merge(ecw_series, left_on=['name','index'],right_index=True)
  
  can not merge DataFrame with instance of type &lt;class 
  'pandas.core.series.Series'
  </code></pre>
  
  <p>At this point, I was considering converting the series to a dataframe, and then merging. But I am sure there must be a better way.</p>