title: Faster way to transform group with mean value in Pandas
body: <p>I have a Pandas dataframe where I am trying to replace the values in each group by the mean of the group. On my machine, the line <code>df["signal"].groupby(g).transform(np.mean)</code> takes about 10 seconds to run with <code>N</code> and <code>N_TRANSITIONS</code> set to the numbers below.</p>
  
  <p>Is there any faster way to achieve the same result?</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  from time import time
  
  np.random.seed(0)
  
  N = 120000
  N_TRANSITIONS = 1400
  
  # generate groups
  transition_points = np.random.permutation(np.arange(N))[:N_TRANSITIONS]
  transition_points.sort()
  transitions = np.zeros((N,), dtype=np.bool)
  transitions[transition_points] = True
  g = transitions.cumsum()
  
  df = pd.DataFrame({ "signal" : np.random.rand(N)})
  
  # here is my bottleneck for large N
  tic = time()
  result = df["signal"].groupby(g).transform(np.mean)
  toc = time()
  print toc - tic
  </code></pre>