Chris90
Chris90

Reputation: 1998

Calculating Quantiles based on a column value?

I am trying to figure out a way in which I can calculate quantiles in pandas or python based on a column value? Also can I calculate multiple different quantiles in one output?

For example I want to calculate the 0.25, 0.50 and 0.9 quantiles for

Column Minutes in df where it is <= 5 and where it is > 5 and <=10

df[df['Minutes'] <=5]

df[(df['Minutes'] >5) & (df['Minutes']<=10)]

where column Minutes is just a column containing value of numerical minutes

Thanks!

Upvotes: 0

Views: 346

Answers (1)

Vaishali
Vaishali

Reputation: 38415

DataFrame.quantile accepts values in array,

Try

df['minute'].quantile([0.25, 0.50 , 0.9])

Or filter the data first,

df.loc[df['minute'] <= 5, 'minute'].quantile([0.25, 0.50 , 0.9])

Upvotes: 3

Related Questions