Calculate Arbitrary Percentile on Pandas GroupBy

后端 未结 3 1492
-上瘾入骨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:11

    With pandas >= 0.25.0 you can also use Named aggregation

    An example would be

    import numpy
    import pandas as pd
    df = pd.DataFrame({'A': numpy.random.randint(1,3,size=100),'C': numpy.random.randn(100)})
    df.groupby('A').agg(min_val = ('C','min'), percentile_80 = ('C',lambda x: x.quantile(0.8)))
    

提交回复
热议问题