Python 从基于百分位数的数据框中排除数据

Python 从基于百分位数的数据框中排除数据,python,pandas,Python,Pandas,在生成散点图之前,我很难尝试使用Python删除一些数据异常值。我使用pandas导入了一个n×43的数据帧。我已经知道了如何确定异常值的阈值,并将其应用到数据帧中,这样我现在就有了一些布尔值,对应于数据是否应该包含在散点图中。然而,我一直在思考如何使用这些信息来排除适当的数据点 到目前为止,我的代码是: def identify_outliers(self,parameters_file): data=pandas.read_csv(parameters_file) #i

在生成散点图之前,我很难尝试使用Python删除一些数据异常值。我使用pandas导入了一个n×43的数据帧。我已经知道了如何确定异常值的阈值,并将其应用到数据帧中,这样我现在就有了一些布尔值,对应于数据是否应该包含在散点图中。然而,我一直在思考如何使用这些信息来排除适当的数据点

到目前为止,我的代码是:

def identify_outliers(self,parameters_file):
    data=pandas.read_csv(parameters_file)        #import data
    header=data.keys()                           #get header
    quantiles = data.quantile([0.25,0.75],1)     #determine thresholds for all data 
    for i in range(len(header)):
        qnt_i = quantiles[header[i]].as_matrix() #get handle to quantiles 
                                                 #for specific column of data
        #identify data points that fall outside this range   
        boolean_data=data[header[i]].between(qnt_i[0],qnt_i[1])  
        for j in range(len(boolean_data)): #attempt to use boolean values to filter
                                           #data to only include 'True' (doesn't work)
            if boolean_data[j]:
                print data[header[i]]
下面是使用pandas.read\u csv导入的数据片段

(v1).Kcat   (v1).km     (v11).k1
1.22E-02    1.20E-02    1.72E-06
0.0122441   1.42E-02    1.61E-06
1.04E-02    1.01E-02    1.00E-06
0.0136581   0.0185623   5.01158
0.0113221   0.0221445   0.0785929
0.506949    0.01        1.35E-06
1.16567     0.0141031   168.078
0.01        0.0100055   1.25E-06
0.0351003   153.682     163.082
0.0129821   0.0164996   0.0560866

是否有人建议我如何过滤“数据”以删除所有不在指定范围内的值。

这里是一个仅基于前5行的较小数据帧

df

         v1          v2          v3
0  0.012200    0.012000    0.000002
1  0.012244    0.014200    0.000002
2  0.010400    0.010100    0.000001
3  0.013658    0.018562    5.011580
4  0.011322    0.022145    0.078593
这是一个仅选择25%和75%之间的值的掩码。注意,它的语法有点精确,所以要小心使用括号等

 ( df > df.quantile(.25) ) & ( df < df.quantile(.75) )

      v1     v2     v3
0   True   True   True
1   True   True   True
2  False  False  False
3   True  False  False
4  False  False   True
(df>df.分位数(.25))和(df
顺便说一句,这是基于列的。我只是快速浏览了一下您的代码,不容易判断百分位度量是否是针对每列的3列的组合。对于整个数据帧,您可以执行以下操作:

( df > df.stack().quantile(.25) ) & ( df < df.stack().quantile(.75) )
(df>df.stack().quantile(.25))和(df
我想要的答案是:

def identify_outliers(self,parameters_file):
    data=pandas.read_csv(parameters_file)
    header=data.keys()
    quantiles = data.quantile([0.25,0.75],1) 
    cols=data.shape[1]
    rows=data.shape[0]
    boolean_data=[]
    for i in range(len(header)):
        qnt_i = quantiles[header[i]].as_matrix()
        print data[header[i]][(qnt_i[0]<data[header[i]])&(data[header[i]]<qnt_i[1])]
def识别异常值(自身、参数文件):
data=pandas.read\u csv(参数\u文件)
header=data.keys()
分位数=数据分位数([0.25,0.75],1)
cols=data.shape[1]
行=数据。形状[0]
布尔值_数据=[]
对于范围内的i(len(header)):
qnt_i=分位数[header[i]]。作为_矩阵()

打印数据[header[i][(qnt_i[0]运行代码后,能否指定当前df中的列?具体来说,显示哪些列是原始数据和布尔列。