pythonpandasdataframequantile

Turning a quantile calculation output into a dataframe


I am trying to calculate quantile output and am close, but not sure how to turn my output into a data frame.

x.groupby(['day'])['mins'].quantile(.5)

This gives me what I want,

The output isn't a data frame and I needed the output to be a data frame.

Output looks like:

    day
    2019-06-28    3.0
    2019-06-30    4.0
    2019-07-02    3.0
    2019-07-06    3.0
    2019-07-08    3.0
    Name: mins, dtype: float64

Solution

  • you just need to do reset_index()

    df = pd.DataFrame({'mins': [1, 2, 2, 10, 6],
                       'day':['2019-06-28','2019-06-28','2019-06-30','2019-06-30','2019-07-02']})
    
    res = df.groupby(['day'])['mins'].quantile(0.5).reset_index()
    res.rename(columns={'mins':'quantile_value'},inplace=True)
    print(res)
    
              day  quantile_value
    0  2019-06-28   1.5
    1  2019-06-30   6.0
    2  2019-07-02   6.0
    

    I hope it would solve your problem