Calculate Arbitrary Percentile on Pandas GroupBy

后端 未结 3 1489
-上瘾入骨i
-上瘾入骨i 2020-12-14 02:09

Currently there is a median method on the Pandas\'s GroupBy objects.

Is there is a way to calculate an arbitrary percentile (s

3条回答
  •  别那么骄傲
    2020-12-14 02:24

    I found another useful solution here

    If I have to use groupby another approach can be:

    def percentile(n):
        def percentile_(x):
            return np.percentile(x, n)
        percentile_.__name__ = 'percentile_%s' % n
        return percentile_
    

    Using the below call, I am able to achieve the same result as the solution given by @TomAugspurger

    df.groupby('C').agg([percentile(50), percentile(95)])

提交回复
热议问题