quantile()

quantile()

确定四分位数的位置有两种方法, 方法1 pos = (n+1)p 方法2 pos = 1+(n-1)p

pandas 中使用的是方法2确定的。默认使用linear插值

In [213]:
df
 
 
Out[213]:
 key1key2data1data2
0 a one -0.594168 0.530619
1 a two -1.337130 0.619927
2 b one -1.700586 0.464591
3 b two -0.399619 -0.211291
4 a one -0.277584 0.668908
In [217]:
 df.quantile(0.1)
 
Out[217]:
data1   -1.555204
data2    0.059062
Name: 0.1, dtype: float64
In [ ]:
 
#默认使用的是linear插值
#data1列
#pos=1+(5-1)*0.1=1.4 fac=0.4,  -1.700586+(-1.337130-(-1.700586))*0.4=-1.555204
In [229]:
df.quantile([0.05,0.95])  #注意中括号
 
 
Out[229]:
 data1data2
0.05 -1.627895 -0.076115
0.95 -0.301991 0.659112
In [260]:
 
def cap_outliers(ser,lower,higher):
    low,high=ser.quantile([lower,higher])
    ser[ser<low]=low
    ser[ser>high]=high
    return (ser)
cap_outliers(df['data1'],0.05,0.95)
 
Out[260]:
0   -0.594168
1   -1.337130
2   -1.523220
3   -0.399619
4   -0.337137
Name: data1, dtype: float64
原文地址:https://www.cnblogs.com/liyun1/p/11261878.html