calculate within categories: equivalent of r's ddply in python?,df.groupby('d').apply(f)