본문 바로가기

Python/Pandas

Pandas _ 이상치 제외 방법



df_list = []
for i in range(2015, 2018):
    df_list.append(
        pd.read_csv("{}.csv".format(i))
    )

df = pd.concat(df_list)
df = df.dropna()

df['rtn'] = df['price2']/df['price'] - 1

# 이상치 제거

for col in df.columns:
    if col not in ['ticker', 'price2', 'price', 'trn']:
        mu = df[col].mean()
        std = df[col].std()
        
        cond1 = mu - 2*std <= df[col]
        cond2 = df[col] <= mu + 2*std
        
        df = df[cond1 & cond2]

'Python > Pandas' 카테고리의 다른 글

Pandas _ 데이터 병합 예시  (0) 2021.01.16
Pandas _ join & merge  (0) 2021.01.16
Pandas _ concat & pivot  (0) 2021.01.13
Pandas _ row 추가하기 (loc & append)  (0) 2021.01.12
Pandas _ qcut(균할분등) & groupby()  (0) 2021.01.11