2017-09-15 2 views
2

팬더 데이터 프레임이 주어지면, 필자는 하나의 컬럼을 기준으로 아웃 라이어 (Z 값 = 3)에 해당하는 행을 제외하고자합니다.팬더 데이터 프레임 - 아웃 라이어 제거

dff=df[(np.abs(stats.zscore(df)) < 3).all(axis='_source.price')] 

다음 예외가 발생 :

-------------------------------------------------------------------------  
TypeError         Traceback (most recent call last) 
<ipython-input-68-02fb15620e33> in <module>() 
----> 1 dff=df[(np.abs(stats.zscore(df)) < 3).all(axis='_source.price')] 

/opt/anaconda3/lib/python3.6/site-packages/scipy/stats/stats.py in zscore(a, axis, ddof) 
    2239  """ 
    2240  a = np.asanyarray(a) 
-> 2241  mns = a.mean(axis=axis) 
    2242  sstd = a.std(axis=axis, ddof=ddof) 
    2243  if axis and mns.ndim < a.ndim: 

/opt/anaconda3/lib/python3.6/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims) 
    68    is_float16_result = True 
    69 
---> 70  ret = umr_sum(arr, axis, dtype, out, keepdims) 
    71  if isinstance(ret, mu.ndarray): 
    72   ret = um.true_divide(

TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' 

그리고

의 반환 값 라인의 경우

df.dtypes 
_id     object 
_index    object 
_score    object 
_source.address  object 
_source.district  object 
_source.price  float64 
_source.roomCount float64 
_source.size   float64 
_type     object 
sort     object 
priceSquareMeter  float64 
dtype: object 

:

dataframe은 다음과 같습니다 10

True 

내가 위의 예외를 이유는 무엇입니까

, 그리고 내가 어떻게 이상 값을 제외 할 수 있습니까?

답변

2

사용은 문제의이 종류가 때마다이 부울 :

df=pd.DataFrame({'Data':np.random.normal(size=200)}) #example 
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 the other way around 
0

난 당신이 아웃 라이어와 부울 필터를 만들고 그것의 oposite을 선택할 수있다 생각합니다.

outliers = stats.zscore(df['_source.price']).apply(lambda x: np.abs(x) == 3) 
df_without_outliers = df[~outliers]