pandasiqr

Python: calculating the 2 and 3 quartiles per group in a dataset


I have the following dataframe:

df =
code count
A    1
A    2
A    3
A    4
B    7
B    8
B    9
B    10

I want to produce the 25 and 75 quartiles per code:

code 25QT 75QT
A    2.5   3.5
B    8.5   9.5

Solution

  • Use groupby.quantile, then unstack:

    df.groupby('code')['count'].quantile([0.25, 0.75]).unstack()
    

    Output:

          0.25  0.75
    code            
    A     1.75  3.25
    B     7.75  9.25
    

    With the format:

    out = (df.groupby('code')['count']
             .quantile([0.25, 0.75]).unstack()
             .rename(columns=lambda x: f'{int(x*100)}QT')
             .reset_index()
          )
    

    Output:

      code  25QT  75QT
    0    A  1.75  3.25
    1    B  7.75  9.25