0 votes
1 view
in Data Science by (17.6k points)

I am trying to calculate quantile output and am close, but not sure how to turn my output into a df that I can access as a df.

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

This gives me what I want,

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

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

Thanks!

1 Answer

0 votes
by (38.9k points)

Here, you should use 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

 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...