I have a DF [named cleanData] with some values and 2 columns which are custom_critirea and total_count.
Here is a section of my DF :
CUSTOM_CRITERIA TOTAL_CODE_SERVED_COUNT
8 2768012 27
9 3307322 1
10 3270374 2
11 3353569 4
12 3423432 660
13 1737751 0
14 3564415 5
15 3593988 1
16 3593981 2
17 3603423 48367
18 3483162 6
19 3603380 3
20 3483062 2
21 3617505 2363
22 3617633 11
23 3607897 7
24 3619532 1
28 3633518 3
29 3653760 22
30 3653625 ...
What I have right now is this :
aggMap = {'TOTAL_CODE_SERVED_COUNT': ['sum', 'max']}
cleanData = cleanData.groupby('CUSTOM_CRITERIA').agg(aggMap)
This gives me the max and sum of the TOTAL CODE SERVED COUNT PER CUSTOM CRITERIA.
What I want to achieve now is getting the second-highest value from the aggregation
I need something like this :
# myfunc should return for each group the second highest TOTAL_CODE_SERVED_COUNT
aggMap = {'TOTAL_CODE_SERVED_COUNT': ['sum', myfunc]}
cleanData = cleanData.groupby('CUSTOM_CRITERIA').agg(aggMap)
Can it be achieved using df.groupby().agg() ?