Nilton
Nilton

Reputation: 47

How to assign to a variable the outputs of the method "describe"?

good morning!

Could you help me, please, teaching me how to assign to a variable the outputs of the method "describe"?

enter image description here

Thanks and have a great day!

Upvotes: 1

Views: 577

Answers (2)

Scott Boston
Scott Boston

Reputation: 153500

pd.DataFrame.describe returns a dataframe, you can use loc to access each cell of a dataframe or you can calculate the stat directly.

import pandas as pd
from seaborn import load_dataset

df_tips =  load_dataset('tips')
print(df_tips.describe())

Output:

       total_bill         tip        size
count  244.000000  244.000000  244.000000
mean    19.785943    2.998279    2.569672
std      8.902412    1.383638    0.951100
min      3.070000    1.000000    1.000000
25%     13.347500    2.000000    2.000000
50%     17.795000    2.900000    2.000000
75%     24.127500    3.562500    3.000000
max     50.810000   10.000000    6.000000

Getting 25%:

df_tips.describe().loc['25%', 'total_bill']
#or
df_tips['total_bill'].quantile(.25)

Output:

13.3475

Getting 50%:

df_tips.describe().loc['50%', 'total_bill']
#or
df_tips['total_bill'].quantile(.50)

Output:

17.795

Upvotes: 3

Hoori M.
Hoori M.

Reputation: 730

The 25% and 50% are quantiles so you can simply use the pandas quantile function to get those values.

For all the information you see in describe output, you have functions in pandas.DataFrame that you can work with, e.g.:

count -> pandas.DataFrame.count
mean -> pandas.DataFrame.mean
std -> pandas.DataFrame.std
min -> pandas.DataFrame.min
25%, 50%, 75% or any other quantile -> pandas.DataFrame.quantile
max -> pandas.DataFrame.max

Upvotes: 1

Related Questions