title: Python pandas: how to run multiple univariate regression by group
body: <p>Suppose I have a <code>DataFrame</code> with one column of <code>y</code> variable and many columns of <code>x</code> variables. I would like to be able to run multiple univariate regressions of <code>y</code> vs <code>x1</code>, <code>y</code> vs <code>x2</code>, ..., etc, and store the predictions back into the <code>DataFrame</code>. Also I need to do this by a group variable.</p>
  
  <pre><code>import statsmodels.api as sm
  import pandas as pd
  
  df = pd.DataFrame({
    'y': np.random.randn(20),
    'x1': np.random.randn(20), 
    'x2': np.random.randn(20),
    'grp': ['a', 'b'] * 10})
  
  def ols_res(x, y):
      return sm.OLS(y, x).fit().predict()
  
  df.groupby('grp').apply(ols_res) # This does not work
  </code></pre>
  
  <p>The code above obviously does not work. It is not clear to me how to correctly pass the fixed <code>y</code> to the function while having <code>apply</code> iterating through the <code>x</code> columns(<code>x1</code>, <code>x2</code>, ...). I suspect there might be a very clever one-line solution to do this. Any idea?</p>