【发布时间】: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) <= nb <= (@Q3 + 1.5 * @IQR)')不起作用?还是有什么问题? -
此解决方案适用于特定列,我想在整个数据框中执行它。这就是我感到困惑的地方,
-
如果某列中有异常值,是否需要删除所有行?
-
我认为需要
this解决方案 -
你能查一下
df = stepframe[~((stepframe < (Q1 - 1.5 * IQR)) | (stepframe > (Q3 + 1.5 * IQR))).any(axis=1)]吗?
标签: python-3.x pandas dataframe iqr