python-3.x - 如何使用 IQR 从 DataFrame 中删除异常值?

标签 python-3.x pandas dataframe iqr

我的数据框有很多列(大约 100 个特征),我想应用四分位数方法并希望从数据框中删除异常值。

我正在使用此链接 stackOverflow

但问题是上述方法无法正常工作,

正如我正在尝试的那样

Q1 = stepframe.quantile(0.25)
Q3 = stepframe.quantile(0.75)
IQR = Q3 - Q1
((stepframe < (Q1 - 1.5 * IQR)) | (stepframe > (Q3 + 1.5 * IQR))).sum()

它给了我这个

((stepframe < (Q1 - 1.5 * IQR)) | (stepframe > (Q3 + 1.5 * IQR))).sum()
Out[35]: 
Day                      0
Col1                     0
Col2                     0
col3                     0
Col4                     0
Step_Count            1179
dtype: int64

我只是想知道,接下来我要做什么,以便删除数据框中的所有异常值。

如果我使用这个

def remove_outlier(df_in, col_name):
q1 = df_in[col_name].quantile(0.25)
q3 = df_in[col_name].quantile(0.75)
iqr = q3-q1 #Interquartile range
fence_low  = q1-1.5*iqr
fence_high = q3+1.5*iqr
df_out = df_in.loc[(df_in[col_name] > fence_low) & (df_in[col_name] < fence_high)]
return df_out

re_dat = remove_outlier(stepframe, stepframe.columns)

我收到此错误

ValueError: Cannot index with multidimensional key

在这一行

    df_out = df_in.loc[(df_in[col_name] > fence_low) & (df_in[col_name] < fence_high)]

最佳答案

您可以使用:

np.random.seed(33454)
stepframe = pd.DataFrame({'a': np.random.randint(1, 200, 20), 
                          'b': np.random.randint(1, 200, 20),
                          'c': np.random.randint(1, 200, 20)})

stepframe[stepframe > 150] *= 10
print (stepframe)

Q1 = stepframe.quantile(0.25)
Q3 = stepframe.quantile(0.75)
IQR = Q3 - Q1

df = stepframe[~((stepframe < (Q1 - 1.5 * IQR)) |(stepframe > (Q3 + 1.5 * IQR))).any(axis=1)]

print (df)
      a    b     c
1   109   50   124
3   137   60  1990
4    19  138   100
5    86   83   143
6    55   23    58
7    78  145    18
8   132   39    65
9    37  146  1970
13   67  148  1880
15  124  102    21
16   93   61    56
17   84   21    25
19   34   52   126

详细信息:

首先创建boolean DataFrame带链条| :

print (((stepframe < (Q1 - 1.5 * IQR)) | (stepframe > (Q3 + 1.5 * IQR))))
        a      b      c
0   False   True  False
1   False  False  False
2    True  False  False
3   False  False  False
4   False  False  False
5   False  False  False
6   False  False  False
7   False  False  False
8   False  False  False
9   False  False  False
10   True  False  False
11  False   True  False
12  False   True  False
13  False  False  False
14  False   True  False
15  False  False  False
16  False  False  False
17  False  False  False
18  False   True  False
19  False  False  False

然后使用 DataFrame.any 检查至少一项 True每行和最后一个反转 bool 掩码 ~ :

print (~((stepframe < (Q1 - 1.5 * IQR)) | (stepframe > (Q3 + 1.5 * IQR))).any(axis=1))
0     False
1      True
2     False
3      True
4      True
5      True
6      True
7      True
8      True
9      True
10    False
11    False
12    False
13     True
14    False
15     True
16     True
17     True
18    False
19     True
dtype: bool
<小时/>

invert条件改变后的解决方案 - <>=><= ,链由&对于 AND 和最后一个过滤器 all 查看全部True每行数

print (((stepframe >= (Q1 - 1.5 * IQR)) & (stepframe <= (Q3 + 1.5 * IQR))).all(axis=1))
0     False
1      True
2     False
3      True
4      True
5      True
6      True
7      True
8      True
9      True
10    False
11    False
12    False
13     True
14    False
15     True
16     True
17     True
18    False
19     True
dtype: bool


df = stepframe[((stepframe >= (Q1 - 1.5 * IQR))& (stepframe <= (Q3 + 1.5 * IQR))).all(axis=1)]

关于python-3.x - 如何使用 IQR 从 DataFrame 中删除异常值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59815681/

相关文章:

Python:删除集合中字符串的较长子字符串

python-3.x - 将 pi 的 opencv 视频传输到 ffmpeg 以进行 Youtube 流式传输

python - 尝试在每个数据帧列条目中搜索与 'Id' 对应的值时出现内存错误

python - np.where 多个逻辑语句 pandas

python - 查找一列中与另一列中的某些值关联的值

python - 基于 Pandas.Dataframe 中的多列合并多个重复行

python - 使用 Python SDK for couchbase 时出现 std::bad_cast 错误

python - 将两个不同数据框中每一行的值相乘

python - 如何使用 Pandas (Python) 报告用户-项目交互

python - 我可以在 python 中传递 if 循环的自定义条件语句吗?