import pandas as pd
import numpy as np
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 pd.Series(sm.OLS(y, x).fit().predict())
df.groupby('grp').apply(lambda x: x[['x1', 'x2']].apply(ols_res, y=x['y']))
Python pandas: how to run multiple univariate regression by group