quantile python code example
Example 1: calculate percentile pandas dataframe
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
df.field_A.mean()
df.field_A.median()
df.field_A.quantile(0.1)
df.field_A.quantile(0.5)
df.field_A.quantile(0.9)
Example 2: how to sort values of pandas dataframe for iqr
def mod_outlier(df):
df1 = df.copy()
df = df._get_numeric_data()
q1 = df.quantile(0.25)
q3 = df.quantile(0.75)
iqr = q3 - q1
lower_bound = q1 -(1.5 * iqr)
upper_bound = q3 +(1.5 * iqr)
for col in col_vals:
for i in range(0,len(df[col])):
if df[col][i] < lower_bound[col]:
df[col][i] = lower_bound[col]
if df[col][i] > upper_bound[col]:
df[col][i] = upper_bound[col]
for col in col_vals:
df1[col] = df[col]
return(df1)
Example 3: np.quantile
numpy.quantile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)
Example 4: calculate quantiles python
>>> np.percentile(df.time_diff, 25)
0.48333300000000001
>>> np.percentile(df.time_diff, 50)
0.5
>>> np.percentile(df.time_diff, 75)
0.51666699999999999