Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数删除python中的异常值_Python_Python 3.x_Python 2.7_Pandas_Statistics - Fatal编程技术网

函数删除python中的异常值

函数删除python中的异常值,python,python-3.x,python-2.7,pandas,statistics,Python,Python 3.x,Python 2.7,Pandas,Statistics,我正在尝试编写一个函数,用四分位数区间更新数据集中所有列中的所有异常值。当我将一列作为输入传递时,它是工作的,但是如果我添加另一个循环来迭代所有列,它就不工作了 df2ColumnNames=df2.columns def fixoutliers(x): for i in df2ColumnNames: print("colnames ",i) xy=x[i] print(xy) updated=[] Q1,Q3=np.percentile(x

我正在尝试编写一个函数,用
四分位数区间更新
数据集中所有列中的所有异常值。当我将一列作为输入传递时,它是工作的,但是如果我添加另一个循环来迭代所有列,它就不工作了

df2ColumnNames=df2.columns

def fixoutliers(x):

for i in df2ColumnNames:
    print("colnames ",i)
    xy=x[i]    
    print(xy)
    updated=[]
    Q1,Q3=np.percentile(xy,[25,75])
    IQR=Q3-Q1
    #print(IQR)
    minimum=Q1-1.5*IQR
    maximum=Q3+1.5*IQR
    print("maximum",maximum)
    for i in xy:
        if(i>maximum):
            i=maximum
            updated.append(i)
        elif(i<minimum):
            i=minimum
            updated.append(i)
        else:
            print("In else")
            updated.append(i)
    return updated
df2ColumnNames=df2.columns
def固定值异常值(x):
对于df2ColumnNames中的i:
打印(“colnames”,i)
xy=x[i]
打印(xy)
更新=[]
Q1,Q3=np百分位(xy,[25,75])
IQR=Q3-Q1
#打印(IQR)
最小值=Q1-1.5*IQR
最大值=Q3+1.5*IQR
打印(“最大值”,最大值)
对于xy中的i:
如果(i>最大值):
i=最大值
更新。追加(i)

elif(i由于箱线图也使用相同的理论“四分位范围”来检测异常值,因此您可以直接使用它来查找数据帧上的异常值

import pandas as pd

_, bp = pd.DataFrame.boxplot(df2, return_type='both')
outliers = [flier.get_ydata() for flier in bp["fliers"]]
out_liers = [i.tolist() for i in outliers]

谢谢大家的建议。经过一点努力,我成功地创建了我想要的功能。如果对某人有帮助,请发布解决方案

#####定义一个函数,该函数接受一个可以 包含数字列和分类列######

def固定值异常值(x):

##从输入数据帧x获取所有列名
xColumnNames=x.columns
打印(X列名称)
#对于DF2列名称中的j:
对于xColumnNames中的j:
尝试:
打印(“colnames”,j)
xy=x[j]
mydata=pd.DataFrame()
#打印(xy)
更新=[]
Q1,Q3=np百分位(xy,[25,75])
IQR=Q3-Q1
最小值=Q1-1.5*IQR
最大值=Q3+1.5*IQR
对于xy中的i:
如果(i>最大值):
打印(“输入格言”)
i=最大值
更新。追加(i)

elif(iYou可能还需要添加
numpy
pandas
标记。基于百分位的异常值检测存在问题。感谢链接…我将在处理异常值之前正确检查我的分布
##Get all the column name from the input dataframe x
xColumnNames=x.columns
print(xColumnNames)
#for j in df2ColumnNames:

for j in xColumnNames:
    try:
        print("colnames ",j)
        xy=x[j]    
        mydata=pd.DataFrame()
        #print(xy)
        updated=[]
        Q1,Q3=np.percentile(xy,[25,75])
        IQR=Q3-Q1
        minimum=Q1-1.5*IQR
        maximum=Q3+1.5*IQR
        for i in xy:
            if(i>maximum):
                print("Entering maxim")
                i=maximum
                updated.append(i)
            elif(i<minimum):
                print("enterinf minimum")
                i=minimum
                updated.append(i)
            else:
                updated.append(i)
        x[j]=updated
    except:
        continue
return x