LoginSignup
10
14

More than 5 years have passed since last update.

【pandas】四分位数を求めて外れ値を検出する

Last updated at Posted at 2016-12-02

外れ値の検出で四分位数を利用したいことがあったのでメモ

seriesの四分位数を取得する

Q1 = series.quantile(.25)
Q3 = series.quantile(.75)

もしくは

Q1 = series.describe()['25%']
Q3 = series.describe()['75%']

四分位数を用いた外れ値の検出

# A列で値が外れているデータのみ抽出
IQR = Q3 - Q1
threshold = Q3 + 1.5 * IQR

df_outlier = df[df['A'].apply(lambda x:x > threshold)]

逆に収まっているデータが欲しければdf[~df...]みたいに論理否定を取ったり
不等号の向きを変えたりすれば良さげ。

まとめ

こんなことしなくても一発で外れ値出せるやつとかありそうですが...

統計初心者がpandasを使ってデータをいじくりまわしております。
何かいい方法あったら教えていただけるとうれしいです。

参考

四分位数についてはこの資料を参考に
http://www.contents-station.net/gacco/Data_Analysis_Innovation/Week03/3-4.pdf

10
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
14