2017-01-09 62 views
2

說我有一個特點和標籤一個數據幀:過濾離羣,除了一個

f1 f2 label 
-1000 -100 1 
-5 3 2 
0  4 3 
1  5 1 
3  6 1 
1000 100 2 

我想過濾來自列f1和f2離羣獲得:

f1 f2 label 
-5 3 2 
0  4 3 
1  5 1 
3  6 1 

我知道我可以這樣做:

data = data[(data > data.quantile(.05)) & (data < data.quantile(.95))] 

但'標籤'列也將被過濾。我怎樣才能避免過濾一些列?我不想手動過濾所有列,因爲有幾十個。 謝謝。

回答

1

有關下列方法是什麼:

In [306]: x = data.drop('label', 1) 

In [307]: x.columns 
Out[307]: Index(['f1', 'f2'], dtype='object') 

In [308]: data[((x > x.quantile(.05)) & (x < x.quantile(.95))).all(1)] 
Out[308]: 
    f1 f2 label 
1 -5 3  2 
2 0 4  3 
3 1 5  1 
4 3 6  1 
+1

順便說一句,你可能會發現這些有用[組內cumcount(http://stackoverflow.com/a/41558148/2336654)和[cummax組內(HTTP ://stackoverflow.com/a/41526917/2336654) – piRSquared