How can I find the average value of n largest values in a month, but day has to be unique?
I do have a timestamp column as well, but I would guess making columns of them is the way to go?
I tried df['peak_avg'] = df.groupby(['month', 'day'])['value'].transform(lambda x: x.nlargest(3).mean()), but this takes the average of the three largest days.
| month | day | value | peak_avg (expected) |
|---|---|---|---|
| 1 | 1 | 35 | 35 |
| 1 | 1 | 30 | 35 |
| 2 | 1 | 34 | 28.5 |
| 2 | 2 | 23 | 28.5 |
| 3 | 1 | 98 | 97 |
| 3 | 2 | 96. | 97 |