Calculate within categories: Equivalent of R's ddply in Python?,df.groupby('d').apply(f)