import pandas as pd
import pandas.rpy.common as com
mtcars = com.load_data('mtcars')
mtcars['n'] = mtcars.groupby('cyl')['cyl'].transform('count')
print(mtcars.head())
Equivalent of transform in R/ddply in Python/pandas?