有没有一种内置的方法可以根据IQR对列进行过滤(即Q1-1.5IQR和Q3+1.5IQR之间的值)?此外,任何其他可能的广义过滤熊猫建议将不胜感激。
发布于 2016-02-24 23:51:53
据我所知,最紧凑的符号似乎是由query方法带来的。
# Some test data
np.random.seed(33454)
df = (
# A standard distribution
pd.DataFrame({'nb': np.random.randint(0, 100, 20)})
# Adding some outliers
.append(pd.DataFrame({'nb': np.random.randint(100, 200, 2)}))
# Reseting the index
.reset_index(drop=True)
)
# Computing IQR
Q1 = df['nb'].quantile(0.25)
Q3 = df['nb'].quantile(0.75)
IQR = Q3 - Q1
# Filtering Values between Q1-1.5IQR and Q3+1.5IQR
filtered = df.query('(@Q1 - 1.5 * @IQR) <= nb <= (@Q3 + 1.5 * @IQR)')然后我们可以绘制结果来检查差异。我们观察到左侧箱线图中的异常值(183处的十字)不再出现在过滤后的序列中。
# Ploting the result to check the difference
df.join(filtered, rsuffix='_filtered').boxplot()

因为这个答案,我已经写了一个关于这个主题的,你可以找到更多的信息。
发布于 2017-04-07 21:32:01
另一种使用Series.between()的方法
iqr = df['col'][df['col'].between(df['col'].quantile(.25), df['col'].quantile(.75), inclusive=True)]抽出:
# Select the first quantile
q1 = df['col'].quantile(.25)
# Select the third quantile
q3 = df['col'].quantile(.75)
# Create a mask inbeetween q1 & q3
mask = df['col'].between(q1, q3, inclusive=True)
# Filtering the initial dataframe with a mask
iqr = df.loc[mask, 'col']发布于 2016-10-31 19:44:57
这将为您提供位于列column的IQR中的df子集
def subset_by_iqr(df, column, whisker_width=1.5):
"""Remove outliers from a dataframe by column, including optional
whiskers, removing rows for which the column value are
less than Q1-1.5IQR or greater than Q3+1.5IQR.
Args:
df (`:obj:pd.DataFrame`): A pandas dataframe to subset
column (str): Name of the column to calculate the subset from.
whisker_width (float): Optional, loosen the IQR filter by a
factor of `whisker_width` * IQR.
Returns:
(`:obj:pd.DataFrame`): Filtered dataframe
"""
# Calculate Q1, Q2 and IQR
q1 = df[column].quantile(0.25)
q3 = df[column].quantile(0.75)
iqr = q3 - q1
# Apply filter with respect to IQR, including optional whiskers
filter = (df[column] >= q1 - whisker_width*iqr) & (df[column] <= q3 + whisker_width*iqr)
return df.loc[filter]
# Example for whiskers = 1.5, as requested by the OP
df_filtered = subset_by_iqr(df, 'column_name', whisker_width=1.5)https://stackoverflow.com/questions/34782063
复制相似问题