I am aggregating 10-20 cols with different functions on them. I want to provide different names to the cols that I'm aggregating. For eg. if this is my database:
A B C.. X Y
1 a 22.. 14 15
1 b 20.. 37 1
1 c 8.. 18 10
1 d 10.. 6 7
2 3e 13.. 4 3
2 f 10.. 12 15
3 g 0.. 5 115
The standard deviation for col X grouped by cols A, B, C, should have field name 'stddev_k' and the mean for col Y grouped by the same cols should have field name 'randname' (The new fieldname cannot be derived from the old field name to which the function is being applied.)
Currently I've solved as follows:
- I run a groupby on one col at a time.
- I then rename the col
- I add the renamed col to dataframe D, which starts as an empty dataframe.
The above 3 run on a for-loop until all the cols I want to aggregate are added to dataframe D
This is a time consuming and lengthy process. I am looking for a faster way to solve this. I found this: Pandas Groupby - naming aggregate output column, which combines my step 2 and 3.
However this solution again works one col at a time and I will still need the time consuming for-loop. Is it possible to rename aggregated cols without doing it one column at a time?
Thanks