mtcars[['n', 'total_wt']] = mtcars.groupby('cyl').apply(lambda x: pd.
    DataFrame({'n': len(x['cyl']), 'total_wt': x['wt'].sum()}, index=x.index))
Equivalent of transform in R/ddply in Python/pandas?