檢測并排除Pandas數(shù)據(jù)幀中的異常值我有一個包含少量列的pandas數(shù)據(jù)幀。現(xiàn)在我知道某些行是基于某個列值的異常值。例如列 - 'Vol'的所有值都大約為12xx,一個值為4000(異常值)?,F(xiàn)在我想排除那些有'Vol'列的行。因此,基本上我需要在數(shù)據(jù)框上放置一個過濾器,以便我們選擇所有行,其中某列的值在與平均值相差3個標(biāo)準(zhǔn)差的范圍內(nèi)。實現(xiàn)這一目標(biāo)的優(yōu)雅方式是什么?
3 回答

揚帆大魚
TA貢獻1799條經(jīng)驗 獲得超9個贊
boolean您可以像使用索引一樣使用索引numpy.array
df = pd.DataFrame({'Data':np.random.normal(size=200)})
# example dataset of normally distributed data.
df[np.abs(df.Data-df.Data.mean()) <= (3*df.Data.std())]
# keep only the ones that are within +3 to -3 standard deviations in the column 'Data'.
df[~(np.abs(df.Data-df.Data.mean()) > (3*df.Data.std()))]
# or if you prefer the other way around
對于一個系列,它是相似的:
S = pd.Series(np.random.normal(size=200))
S[~((S-S.mean()).abs() > 3*S.std())]

波斯汪
TA貢獻1811條經(jīng)驗 獲得超4個贊
對于每個dataframe列,您可以獲得分位數(shù):
q = df["col"].quantile(0.99)
然后過濾:
df[df["col"] < q]
添加回答
舉報
0/150
提交
取消