【问题标题】:How to Remove outlier from DataFrame using IQR?如何使用 IQR 从 DataFrame 中删除异常值?
【发布时间】:2020-05-06 00:19:41
【问题描述】:

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

我正在使用此链接 stackOverflow

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

因为我正在尝试这样

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)]

【问题讨论】:

  • 所以解决方案filtered = df.query('(@Q1 - 1.5 * @IQR) &lt;= nb &lt;= (@Q3 + 1.5 * @IQR)') 不起作用?还是有什么问题?
  • 此解决方案适用于特定列,我想在整个数据框中执行它。这就是我感到困惑的地方,
  • 如果某列中有异常值,是否需要删除所有行?
  • 我认为需要this 解决方案
  • 你能查一下df = stepframe[~((stepframe &lt; (Q1 - 1.5 * IQR)) | (stepframe &gt; (Q3 + 1.5 * IQR))).any(axis=1)]吗?

标签: python-3.x pandas dataframe iqr


【解决方案1】:

你可以使用:

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,最后通过~ 反转布尔掩码:

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 条件改变的解决方案 - &lt;&gt;=&gt;&lt;=,链接 &amp; 用于 AND,最后过滤 all 以检查每行的所有 Trues

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)]

【讨论】:

    【解决方案2】:

    您忘记在引用(['col_name']) 中写下您的专栏名称。

    正确的是:

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

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2015-01-23
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多