Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: find percentile stats of a given column

I have a pandas data frame my_df, where I can find the mean(), median(), mode() of a given column:

my_df['field_A'].mean() my_df['field_A'].median() my_df['field_A'].mode() 

I am wondering is it possible to find more detailed stats such as 90 percentile? Thanks!

like image 322
Edamame Avatar asked Sep 19 '16 20:09

Edamame


People also ask

How do you find the percentile of a pandas series?

Step 1: Define a Pandas series. Step 2: Input percentile value. Step 3: Calculate the percentile. Step 4: Print the percentile.

How do you calculate quantile of a column in Python?

Pandas DataFrame quantile() Method The quantile() method calculates the quantile of the values in a given axis. Default axis is row. By specifying the column axis ( axis='columns' ), the quantile() method calculates the quantile column-wise and returns the mean value for each row.


1 Answers

You can use the pandas.DataFrame.quantile() function, as shown below.

import pandas as pd import random  A = [ random.randint(0,100) for i in range(10) ] B = [ random.randint(0,100) for i in range(10) ]  df = pd.DataFrame({ 'field_A': A, 'field_B': B }) df #    field_A  field_B # 0       90       72 # 1       63       84 # 2       11       74 # 3       61       66 # 4       78       80 # 5       67       75 # 6       89       47 # 7       12       22 # 8       43        5 # 9       30       64  df.field_A.mean()   # Same as df['field_A'].mean() # 54.399999999999999  df.field_A.median()  # 62.0  # You can call `quantile(i)` to get the i'th quantile, # where `i` should be a fractional number.  df.field_A.quantile(0.1) # 10th percentile # 11.9  df.field_A.quantile(0.5) # same as median # 62.0  df.field_A.quantile(0.9) # 90th percentile # 89.10000000000001 
like image 156
stackoverflowuser2010 Avatar answered Sep 23 '22 17:09

stackoverflowuser2010