How to calculate the quartile statistics of a column using the groupby function?

Viewed 51

I have data in 1 min intervals, and I want to change the granularity to 5 mins, and calculate the basic data statistics using .groupby as such:

   df2 = df1.groupby(pd.Grouper(freq='5Min',closed='right',label='right')).agg({
                                        "value1":  "mean", "value2": "max",
                                        "value3": "quantile"})

I want to get quartile/quantile data as well, but can't assign specific quantile point. The default is 50th quantile. How do I get the 75th quantile for value3?

2 Answers

The values you pass to agg don't have to be strings: they can be other functions. You could define a custom function like

def q75(series):
    return series.quantile(0.75)

and then pass this to agg like

   df2 = df1.groupby(pd.Grouper(freq='5Min',closed='right',label='right')).agg({
                                        "value1":  "mean", "value2": "max",
                                        "value3": q75})

You can even calculate multiple quantities for the same stat by passing them in a list:

df2 = df1.groupby(pd.Grouper(freq='5Min', closed='right', label='right')).agg({
    "value1": "mean", "value2": "max", "value3": [q25, q50, q75]})
Related